日期:2014-05-18  浏览次数:20925 次

C# WinForms中 如何实现 操作dos命令
如题, 例如想做一个定时关机程序, 窗体可以获取希望关机时间, WinForms中代码如何 执行dos命令

------解决方案--------------------
Process.Start("...");
------解决方案--------------------
用Windows的API函数:
C# code

[DllImport("shell32.dll")]
 public extern static uint WinExec(string lpCmdLine,uint uCmdShow);

C#使用:

 string lpCmdLine="cmd /c shutdow -s at 18:00";
 WinExec(lpCmdLine,1);

------解决方案--------------------
也可以用过之前写好的bat文件 ,调用这个文件 ,然后只要保证有这个权限 ,就可以进行操作了
------解决方案--------------------
打错了。。
 public static string runcmd(string cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";//设定程序名 
p.StartInfo.Arguments = "/c" + cmd;//设定程序名
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;//重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true;//重定向错误输出
p.StartInfo.CreateNoWindow = false; //设定不显示窗口
p.Start();
return p.StandardOutput.ReadToEnd();
}

------解决方案--------------------
private static void UpdateAddress(string ip, string sub, string master, string dns)
{
string DosCmd = "192.168.6.117" + " " + "255.255.255.0" + " " + "192.168.6.1" + " " + "8.8.8.8";
//System.Diagnostics.Process.Start(Application.StartupPath+"//editnetinfo.bat", DosCmd);
System.Diagnostics.Process.Start("netsh.exe", "interface ip set address 本地连接 static "+ip+" "+sub+" "+master+"");
System.Diagnostics.Process.Start("netsh.exe","netsh interface ip add dns 本地连接 "+" "+dns);
}