Isuru
Isuru

Reputation: 3958

C# Run CMD as Administrator

I'm trying to run cmd command as administrator. But the CMD window closes unexpectedly. If CMD window stays I can see the error. I tried to use process.WaitForExit();

I am trying to run the code zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk as administrator.

Here is my code.

        //The command that we want to run
        string subCommand = zipAlignPath + " -v 4 ";

        //The arguments to the command that we want to run
        string subCommandArgs = apkPath + " release_aligned.apk";

        //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
        //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
        string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\""";

        //Run the runas command directly
        ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");

        //Create our arguments
        string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
        procStartInfo.Arguments = finalArgs;

        //command contains the command to be executed in cmd
        using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
        {
            proc.StartInfo = procStartInfo;
            proc.Start();

        }

Is there a way to keep the CMD window running/showing?

Upvotes: 0

Views: 10724

Answers (3)

mxmillercell
mxmillercell

Reputation: 1

The following method actually works...

private void runCMDFile()
{
    string path = @"C:\Users\username\Desktop\yourFile.cmd";

    Process proc = new Process();                        

    proc.StartInfo.FileName = path;
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.CreateNoWindow = false;
    proc.StartInfo.RedirectStandardOutput = false;
    proc.StartInfo.Verb = "runas";                         
    proc.Start();
    proc.WaitForExit();
}

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613511

You are starting a process from the runas.exe executable file. That's not how to elevate a process.

Instead you need to use shell execute to start your excutable, but use the runas verb. Along these lines:

ProcessStartInfo psi = new ProcessStartInfo(...); // your command here
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);

Upvotes: 1

Dave
Dave

Reputation: 4412

Capture the output(s) from your process:

proc.StartInfo = procStartInfo;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start()
// string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();

Then do something with the output.

Note: you shouldn't try to synchronously read both streams at the same time, as there's a deadlock issue. You can either add asyncronous reading for one or both, or just switch back and forth until you're done troubleshooting.

Upvotes: 0

Related Questions