我已经开发了一个Windows应用程序,其中我有一些实现的功能,现在我想实现优化硬盘,所以我了解defrag.exe。所以我写了一些代码,但它不适合我。谁能做错什么?
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = @"c:\ /A";
try
{
p.Start();
p.WaitForExit();
string a= p.StandardOutput.ToString();
答案 0 :(得分:0)
请参阅我之前发表的评论。除此之外,您需要设置一些额外的参数 - 下面的工作示例。您可能还需要提升权限才能使方案正常工作。如果是这种情况,请参阅this post。
UINavigationController
答案 1 :(得分:0)
添加其他选项 - 尝试以下操作。要使用runas,您需要设置StartInfo.UseShellExecute = true
,这意味着您无法重定向标准输出 - 这仍然适合您吗?另一个选择是以管理员How do I force my .NET application to run as administrator?运行整个程序 - 这将允许您重定向输出并使用提升的权限运行。
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = @"c:\ /A";
// Additional properties set
//p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = true;
//p.StartInfo.CreateNoWindow = true;
p.Start();
// Fixed your request for standard with ReadToEnd
//string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}