我已经开发了一个windows应用程序,在其中我已经实现了一些功能,我想实现优化硬盘,所以我了解了defrag.exe,.so,我写了一些代码,但它不适合我。有人做错什么了吗?
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(); 发布于 2016-05-13 15:02:43
见我对你上一篇文章的评论。除此之外,您还需要设置一些额外的参数--下面是工作示例。您还可能需要提高特权,以使您的方案工作。如果是这样的话,请参见this post。
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 = false;
p.Start();
// Fixed your request for standard with ReadToEnd
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}发布于 2016-05-16 11:38:19
添加另一个选项-试试下面的选项。要使用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();
}https://stackoverflow.com/questions/37213088
复制相似问题