Tob*_*oby 3 c# shutdown process processstartinfo winforms
我想创建一个进程,使用shutdown.exe在给定时间后关闭计算机.
这是我的代码:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "–s –f –t " + seconds;
Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)
其中seconds是int局部变量,用户决定.
当我运行我的代码时没有任何反应.但是当我手动进入cmd提示符并键入:
shutdown.exe - s -f -t 999时,
Windows将弹出一个弹出窗口并告诉我系统将在16分钟内关闭.
我认为这是因为多个参数的原因是我的方法中止正在进行的系统关闭工作(我从cmd提示符手动创建了systemshutdown).这几乎是相同的,除了在startInfo.Argument:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "-a";
Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)
快速检查shutdown.exe的用法消息表明它希望斜杠('/')后面的选项参数不是破折号(' - ').
更换线:
startInfo.Arguments = "–s –f –t " + seconds;
Run Code Online (Sandbox Code Playgroud)
附:
startInfo.Arguments = "/s /f /t " + seconds;
Run Code Online (Sandbox Code Playgroud)
使用C#express 2010在我的盒子上产生一个工作结果.
此外,您可以将程序读取的标准错误和标准错误重定向到已启动的进程中,以便您可以了解运行后发生的情况.为此,您可以存储Process对象并等待基础进程退出,以便您可以检查一切是否顺利.
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Process p = Process.Start(startInfo);
string outstring = p.StandardOutput.ReadToEnd();
string errstring = p.StandardError.ReadToEnd();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法告诉你为什么命令行版本接受选项上的'破折号'前缀而C#执行版本不接受.但是,希望你所追求的是一个有效的解决方案.
以下代码的完整列表:
int seconds = 100;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "/s /f /t " + seconds;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Process p = Process.Start(startInfo);
string outstring = p.StandardOutput.ReadToEnd();
string errstring = p.StandardError.ReadToEnd();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3879 次 |
| 最近记录: |