使用 RedirectStandardOutput 运行 CheckDisk [英] Run CheckDisk with RedirectStandardOutput
问题描述
在不重定向StandardOutput
的情况下运行ChkDsk
,不会出现如下错误:
Running ChkDsk
without redirecting StandardOutput
works without an error like this:
var processStartInfo = new ProcessStartInfo(@"chkdsk.exe", "D:");
processStartInfo.UseShellExecute = false;
var process = Process.Start(processStartInfo);
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("did not work");
重定向 StandardOutput 时,过程以 ExitCode 3
结束.
When redirecting StandardOutput, the process ends with ExitCode 3
.
var processStartInfo = new ProcessStartInfo(@"chkdsk.exe", "D:");
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
var process = Process.Start(processStartInfo);
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("did not work");
做同样的事情,例如net use 和重定向的 StandardOutput 正常工作
Doing the same thing with e.g. net use and redirected StandardOutput works without an error
var processStartInfo = new ProcessStartInfo(@"net ", "use");
为什么?如何执行chkdsk
并重定向StandardOutput
?
Why? How can chkdsk
be executed and StandardOutput
be redirected?
环境:Win 7 Pro x64、UAC 禁用、以管理员身份登录、Dot Net 4.0、VS 2012、WPF 应用程序
Environment: Win 7 Pro x64, UAC disabled, Logged on as Administrator, Dot Net 4.0, VS 2012, WPF Application
推荐答案
您必须以管理员身份运行 Visual Studio,它才会执行,或者您可以添加应用程序清单文件(添加->新项目->常规->应用程序清单)文件)并将这一行 <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
更改为此 <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
它将提示您以管理员身份重新启动 Visual Studio.
You must be running Visual Studio as Administrator and it will execute or you can add a application manifest file(add->new item->general->application manifest file) and changing this line <requestedExecutionLevel level="asInvoker" uiAccess="false" />
to this <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
which will just promp you to restart visual studio as administrator.
这篇关于使用 RedirectStandardOutput 运行 CheckDisk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!