首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何运行Defrag.exe优化windows应用程序c#中的硬盘

如何运行Defrag.exe优化windows应用程序c#中的硬盘
EN

Stack Overflow用户
提问于 2016-05-13 14:46:40
回答 2查看 421关注 0票数 1

我已经开发了一个windows应用程序,在其中我已经实现了一些功能,我想实现优化硬盘,所以我了解了defrag.exe,.so,我写了一些代码,但它不适合我。有人做错什么了吗?

代码语言:javascript
复制
        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(); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-13 15:02:43

见我对你上一篇文章的评论。除此之外,您还需要设置一些额外的参数--下面是工作示例。您还可能需要提高特权,以使您的方案工作。如果是这样的话,请参见this post

代码语言:javascript
复制
 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();
        }
票数 1
EN

Stack Overflow用户

发布于 2016-05-16 11:38:19

添加另一个选项-试试下面的选项。要使用runas,需要设置StartInfo.UseShellExecute = true,这意味着不能重定向标准输出--这对您仍然有效吗?另一种选择是以管理How do I force my .NET application to run as administrator?的形式运行整个程序--这将允许您重定向输出并以提升的权限运行。

代码语言:javascript
复制
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();
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37213088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档