1

Am a Newbie in C# and I have 3 commands(command2, command3 and command4) I need to execute in the elevated command prompt and I will also like to view the execution process as it happens. Currently, the problem is that the code below just opens the elevated command prompt and without executing the commands. I also seek better interpretations of the lines if wrong.

My code and Interpretation/Understanding of each line based on reviews of similar cases: ConsoleApp1

class Program
{
    static void Main(string[] args)
    {
        string command2 = @"netsh wlan"; 
        string command3 = @" set hostednetwork mode=true ssid=egghead key=beanhead keyusage=persistent";
        string command4 = @" start hostednetwork";
        string maincomm = command2.Replace(@"\", @"\\") + " " + command3.Replace(@"\", @"\\") ; //I merged commands 2 and 3

        ProcessStartInfo newstartInfo = new ProcessStartInfo();
        newstartInfo.FileName = "cmd"; //Intend to open cmd. without this the newProcess hits an error saying - Cannot run process without a filename.
        newstartInfo.Verb = "runas"; //Opens cmd in elevated mode
        newstartInfo.Arguments = maincomm; //I intend to pass in the merged commands.
        newstartInfo.UseShellExecute = true; //
        newstartInfo.CreateNoWindow = true; // I intend to see the cmd window 

        Process newProcess = new Process(); //

        newProcess.StartInfo = newstartInfo; //Assigns my newstartInfo to the process object that will execute
        newProcess.Start(); // Begin process and Execute newstartInfo

        newProcess.StartInfo.Arguments = command4; //I intend to overwrite the initial command argument hereby passing the another command to execute.
        newProcess.WaitForExit(); //  
    }
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
jcchuks
  • 881
  • 8
  • 16
  • 1
    What is the exact problem? – Eugene Podskal Aug 11 '14 at 17:28
  • 1
    Did you try to use the command concatenation operator `&&` ? – Steve Aug 11 '14 at 17:29
  • The exact problem is that the code is suppose to enable my wifi but it doesn't. I was thinking I missed something but the commands are alright. when I manually enter them at the elevated command prompt, it works fine but am trying to automate the whole process here and it doesn't work. @EugenePodskal ||. Steve: No I did not try that. I want to be able to enter these commands in that sequence at the command prompt – jcchuks Aug 12 '14 at 09:04

1 Answers1

1

This is what I did to overcome the challenge and It gave me exactly what I wanted. I modified my code to use the System.IO to write directly to the elevated command prompt.

            ProcessStartInfo newstartInfo = new ProcessStartInfo();
            newstartInfo.FileName = "cmd";
            newstartInfo.Verb = "runas";
            newstartInfo.RedirectStandardInput = true;
            newstartInfo.UseShellExecute = false; //The Process object must have the UseShellExecute property set to false in order to redirect IO streams.

            Process newProcess = new Process();

            newProcess.StartInfo = newstartInfo;
            newProcess.Start();
            StreamWriter write = newProcess.StandardInput ; //Using the Streamwriter to write to the elevated command prompt.
            write.WriteLine(maincomm); //First command executes in elevated command prompt
            write.WriteLine(command4); //Second command executes and Everything works fine
            newProcess.WaitForExit();

Referrence: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx

I think an understanding of some properties of the ProcessStartInfo might clear things.

The verb - Gets or sets the verb to use when opening the application or document specified by the FileName property., +The UseShellExecute - Gets or sets a value indicating whether to use the operating system shell to start the process. +The FileName - Gets or sets the application or document to start MSDN Docs

When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. When UseShellExecute is false, you can start only executables by using the Process object Documentation from MSDN.

In my case, cmd is an executable. the verb property is some thing that answers the question "How should my I run my FileName(for executables e.g cmd or any application)?" for which I answered - "runas" i.e run as administrator. When the FileName is a document (e.g `someFile.txt), the verb answers the question "What should I do with the file for which answer(verb) could be -"Edit","print" etc. also?"

use true if the shell should be used when starting the process; false if the process should be created directly from the executable file. The default is true MSDN Docs - UserShellInfo.

Another thing worth noting is knowing what you are trying to achieve. In my case, I want to be able to run commands via an executable(cmd prompt) with the same process - i.e starting the cmd as a process I can keep track of.

jcchuks
  • 881
  • 8
  • 16
  • I would be very curious to know how you got this working (I couldn't) since it seems `UseShellExecute = False` does NOT work with `Verb = "runas"`. See [this SO question](http://stackoverflow.com/questions/3596259/elevating-privileges-doesnt-work-with-useshellexecute-false) on the matter – Mickael V. Sep 10 '15 at 12:38
  • @MickaelV. - I didnt know how best to answer your question so I gave some real lengthy and informative explanation. – jcchuks Oct 23 '15 at 21:18
  • @MickaelV. - I checked your link - the only difference I see is the RedirectStandardInput(set to true) property which Gets or sets a value indicating whether the input for an application is read from the 'Process.StandardInput' stream and also if you check further your will see the line StreamWriter write = newProcess.StandardInput which implies anything I write with/via my 'write' object gets "processed" (not sure if processed is the best word but that's what am trying to imply - sort of ) through newstartInfo. - newProcess.StartInfo = newstartInfo – jcchuks Oct 23 '15 at 21:34