1

I have developed one windows application in which i have some implemented feature now i want to implement optimize hard disk so i got to know about defrag.exe .so i wrote some code but its not working for me. can anyone what am doing wrong ?

        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(); 
Nitesh Sharma
  • 129
  • 2
  • 12

2 Answers2

1

See my comment on your previous post. That aside, you need to set a few extra parameters - working sample below. You may also need to elevate privileges in order for your scenario to work. If this is the case, then see 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();
        }
Community
  • 1
  • 1
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
  • If you are asking me, then yes - it compiled and ran fine in VS2015. – Murray Foxcroft May 13 '16 at 15:41
  • yes wow its working you are awesome :) Thank you very much . but it need to run administrative permission do you know how to give this permission in code so that user don't need to open manually ? – Nitesh Sharma May 14 '16 at 15:10
  • i have added this but its not working – Nitesh Sharma May 14 '16 at 15:14
  • Read my answer - there is a link to a good thread on elevating privileges. – Murray Foxcroft May 15 '16 at 21:47
  • yes i checked that as you can see we are also using startInfo.Verb = "runas" in our code as mentioned above but still its not taking that permission. any idea ? – Nitesh Sharma May 16 '16 at 09:22
  • "runas" will only work if the executing account can elevate privileges to admin. Is this the case? I.e. does the logged in user running the defrag have admin rights? – Murray Foxcroft May 16 '16 at 09:27
  • Also, try decorating your method with the attribute [PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")] this will avoid the elevation prompt you would get with runas. – Murray Foxcroft May 16 '16 at 09:29
  • @ Murray Foxcroft no they are not login as admin rights. this is i want how to get admin rights programmatic – Nitesh Sharma May 16 '16 at 10:05
  • i used that above my method decalartion but when i cal that method then it gives this error- Request for principal permission failed. any idea about this ? – Nitesh Sharma May 16 '16 at 10:09
  • Does the user running your windows application have local administrator permissions? – Murray Foxcroft May 16 '16 at 10:32
0

Adding another option - try the below. To use runas, you need to set StartInfo.UseShellExecute = true which means you cant redirect the standard output - would this still work for you? Another option is to run your whole program as admin How do I force my .NET application to run as administrator? - this will allow you to redirect your output and run with elevated permissions.

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();
  }
Community
  • 1
  • 1
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
  • but i need output so that we can show after completing the process how can we get output string ? – Nitesh Sharma May 18 '16 at 10:08
  • If you need output after the process there there are two solutions. 1) Elevate your entire application when it runs and then use my previous answer. 2) Use this answer and in your args to defrag, redirect the output to the disk and then read the file in, roughly defrag c:\ > c:\temp\result.txt; p.WaitforExit; var result = File.ReadAllLines(c:\temp\result.txt); – Murray Foxcroft May 18 '16 at 10:15
  • 1.do you know how to run entire application application run in administrative permission programmatic ? – Nitesh Sharma May 18 '16 at 10:21
  • 2.Do you know how to redirect the output to the disk and then read the file in, roughly defrag c:\ > c:\temp\result.txt; – Nitesh Sharma May 18 '16 at 10:21
  • I got the solution 1. in manifest was already there 2.set this manifest from property - application tab. Thank you so much murray for your effort and am on skype msp.nitesh lets connect. Thanx again. – Nitesh Sharma May 18 '16 at 12:08