WinForm开发定时关机器的关键代码
来源:http://www.7dit.com 作者:7dit 发布日期:2008-09-21 01:30:18 点击量:485
命名空间 using System.Diagnostics;
主要使用该命名空间内的Process类
以下是关键代码
主要使用该命名空间内的Process类
以下是关键代码
public bool ExecOperate(int type, List<string> fileNames)
{
Process p = new Process();
switch (type)
{
case (int)OperatorType.shutdown:
p.StartInfo.FileName = "cmd.exe"; //处理器执行DOS命令cmd程序
p.StartInfo.UseShellExecute = false; //以下四行设置该程序的运行环境
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -s -f -t 0 -c \"奇点关机器正在关机\""); //关机命令,通过WRITELINE写入相关的Dos命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n关机执行失败");
return false;
}
case (int)OperatorType.reset:
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -r -f -t 0 -c \"奇点关机器正在重启\""); //重启命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n重启执行失败");
return false;
}
case (int)OperatorType.startApp:
for (int i = 0; i < fileNames.Count; i++)
{
try
{
p.StartInfo.FileName = fileNames[i]; //非DOS命令可以直接执行,通过设置StartInfo.FileName
p.Start();
p.Close();
}
catch (Exception)
{
MessageBox.Show("已加载" + Convert.ToString(i) + "个程序\n\n" + fileNames[i] + "执行失败,程序中断");
return false;
}
}
return true;
}
return false;
}
{
Process p = new Process();
switch (type)
{
case (int)OperatorType.shutdown:
p.StartInfo.FileName = "cmd.exe"; //处理器执行DOS命令cmd程序
p.StartInfo.UseShellExecute = false; //以下四行设置该程序的运行环境
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -s -f -t 0 -c \"奇点关机器正在关机\""); //关机命令,通过WRITELINE写入相关的Dos命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n关机执行失败");
return false;
}
case (int)OperatorType.reset:
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("Shutdown.exe -r -f -t 0 -c \"奇点关机器正在重启\""); //重启命令
p.WaitForExit();
p.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show("因异常:" + ex.Message + "\n\n重启执行失败");
return false;
}
case (int)OperatorType.startApp:
for (int i = 0; i < fileNames.Count; i++)
{
try
{
p.StartInfo.FileName = fileNames[i]; //非DOS命令可以直接执行,通过设置StartInfo.FileName
p.Start();
p.Close();
}
catch (Exception)
{
MessageBox.Show("已加载" + Convert.ToString(i) + "个程序\n\n" + fileNames[i] + "执行失败,程序中断");
return false;
}
}
return true;
}
return false;
}
Copyright © 2005 奇点中文网--7dit.com
闽ICP备05001292号
闽ICP备05001292号
