111,125
社区成员
发帖
与我相关
我的任务
分享定时关机系统实现原理:
主要是针对XP系统实现的,通过使用cmd的命令来实现系统的定时关机。
通过Process类来实现调用cmd,用Timer控件来监控系统时间实现时间的匹配。主要代码如下:
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.StandardInput.WriteLine("shutdown -s -t 0");
myProcess.Close();
补充cmd命令:
shutdown -s -t 0 关机命令
shutdown -l -t 0 注销命令
shutdown –a 取消命令
Parallel.For(0, Long.MaxValue, i =>
{
Console.WriteLine("并行接分");
})using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace Caculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Caculate ca = new Caculate();
ca.RegType(textBox1.Text);
ca.Compile();
IA a = ca.Create_A();
textBox2.Text = a.f().ToString() ;
ca.UnloadAdm();
}
}
public interface IA
{
int f();
}
public class Caculate:MarshalByRefObject
{
AppDomain adm;
public void RegType(string str) // 动态注册一个类型
{
StreamWriter sw = new StreamWriter("a.cs");
sw.WriteLine("public class A :System.MarshalByRefObject, Caculator.IA { public int f() { try { return " + str + "; } catch { return int.MinValue; } } } ");
sw.Close();
}
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
// C:\Windows\System32\cmd.exe
//在C盘根目录下 out.txt查看是否编译成功
public void Compile()//编译 下面的字符串写你的CMD 和 编译器路径
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\Windows\System32\cmd.exe";
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
info.Arguments = @"/c C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library " + path + @"\a.cs /r:"+ Assembly.GetExecutingAssembly().Location+@" >E:\out.txt";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();
}
public IA Create_A()//创建A的对象
{
adm = AppDomain.CreateDomain("abc", null, new AppDomainSetup());
return adm.CreateInstanceFromAndUnwrap("a.dll", "A") as IA;
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//Assembly asm = Assembly.LoadFrom(path + @"\a.dll");
//IA a = asm.CreateInstance("A") as IA;
//return a;
}
public void UnloadAdm()
{
AppDomain.Unload(adm);
}
}
}