1

I have been searching and I cannot seem to get this work. I am trying to launch SFC from a button in a C# app. I am aware that it requires rights elevation and in the scope of what I am trying to do is behavior that I want.

I have tried: To run cmd as administrator along with command? Running CMD as administrator with an argument from C# and C# how to run a process(with arguments) through an administrator elevated cmd

The code I tried last was:

private void button6_Click(object sender, EventArgs e)
    {
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + "processNeedToRun")
        {
            RedirectStandardError = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            Verb = "runas"
        };

    }

I either get no process launching at all the cmd window flashes saying incorrect credentials or the command was incorrect.

what am I doing wrong?

Per the comment added I changed it to this:

private void button6_Click(object sender, EventArgs e)
    {
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + "sfc.exe /scannow")
        {
            RedirectStandardError = false,
            RedirectStandardOutput = false,
            UseShellExecute = true,
            CreateNoWindow = false,
            Verb = "runas"
        };

    }

and no change

EDIT:

So I managed to find a solution:

I created a new console app, edited the manifest to require admin and did this

Process.Start("CMD.exe", " /c SFC /Scannow");

That does have the behavior I want. Thanks for the help!

Community
  • 1
  • 1
Chris Iverson
  • 91
  • 1
  • 9

2 Answers2

2

Your console is not elevating, the problem is you can either use the Verb = "runas" and have it elevate or you can use UseShellExecute = false and redirect the outputs, you can not have both.

Your three options are:

  1. Use UseShellExecute = true and disable the redirections
  2. Add a manifest file to your program so it elevates itself as an administrator on start and it can then in turn launch administrative processes and monitor them.
  3. Do a combination of both by writing a 2nd exe that has a manifest file that always runs as administrator. Then have your 1st program launch the 2nd program with Verb = "runas", you then have your 2nd program start SFC with UseShellExecute = false. Your 2nd program then forwards the output via some form of IPC (WCF over named pipes would likely be easiest) to the first unelevated program to be displayed to the user.
Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • second app it is cause neither option 1 or 2 are launching the process. – Chris Iverson Nov 20 '13 at 04:20
  • I don't understand what you are trying to say. – Scott Chamberlain Nov 20 '13 at 04:21
  • I apologize if I am not making myself clear. I tried the first 2 options that you wrote in your first comment and they are not launching the process. As in cmd.exe does not run and I do not get prompted to elevate my rights. – Chris Iverson Nov 20 '13 at 04:25
  • Why are you trying to launch `cmd.exe` at all and not just do `ProcessStartInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System),"sfc.exe"), "/scannow")`? (Be sure you are set to `AnyCPU` when you compile so it gets the correct `System32` folder), and did you try it with `UseShellExecute = true;` and your redirection code commented out? – Scott Chamberlain Nov 20 '13 at 04:28
  • I wanted the command window to show so that progress could be shown. – Chris Iverson Nov 20 '13 at 04:31
0

Try setting ProcesStartInfo.UserName and ProcessStartInfo.Password to impersonate the new process with elevated credentials.

Tilak
  • 30,108
  • 19
  • 83
  • 131