网站风格: 红 | 蓝
搜索:  全站?
首页 |  Java开发 |  DOTNET开发 |  PHP开发 |  ASP技术 |  Web开发 |  数据库应用 |  网络文摘 |  作品集 |  下载 |  留言
文章聚合
  J2EE  |   框架技术  |   Javascript  |   AJAX  |   J2ee Server  |   J2ME  |
  WinForm开发  |   Asp.net  |   C#语言  |   服务器  |   第三方控件  |
  PHP笔记  |   Linux  |   第三方代码  |
  ASP教程  |   ASP实用代码  |   ASP笔记  |   服务器  |
  CSS样式表  |   Photoshop设计  |   Flash  |   HTML 小技巧  |   Dreamweaver  |   XML技术  |
  SQL Server  |   Oracle  |   MySQL  |
  散文  |   幽默  |   杂文  |
  Web 项目  |   杂七杂八  |
  第三方代码  |   实用工具  |   作品下载  |   帮助文档  |
奇点网 - DOTNET开发 - WinForm开发 - WinForm开发定时关机器的关键代码
WinForm开发定时关机器的关键代码
来源:http://www.7dit.com     作者:7dit     发布日期:2008-09-21 01:30:18   点击量:485
命名空间 using System.Diagnostics;

主要使用该命名空间内的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;
}
标签:   C#  |  DOTNET  |  代码  |  WinForm  |
留言 | 链接 | 标签 | 风格: 红 蓝   
Copyright © 2005 奇点中文网--7dit.com
闽ICP备05001292号