Jack
Jack

Reputation: 1

Call Windows Shell and Pass it some arguments .NET c#

Here my problem, I want to use gpg.exe to decrypt some data. Before this, I want to test and make an "ipconfig" through the Windows Shell. I've tried :

Process.Start("cmd","ipconfig");

without success. Did someone know a way to help me please?

Thanks.

Upvotes: 0

Views: 1542

Answers (3)

bob
bob

Reputation: 11

Note that in the statement

ProcessStartInfo("cmd", @"/C " + arguments);

Contrary to the comment in the code, the @ sign only affects the string "/C " and does not the affect the contents of the string arguments. In this case it doesn't hurt anything, but doesn't do anything either.

Contrary to the comment in the code, any \s in arguments would indeed need to be escaped.

Upvotes: 1

Otiel
Otiel

Reputation: 18743

  • The first parameter is the file executed, "cmd" is a shortcut for "C:\Windows\System32\cmd".
  • The second parameter are the arguments to give to the program. Here, you can't just write "ipconfig". You have to use /r or /c or /k to give the arguments to cmd:

    /c or /r : Carries out the command specified by string and then stops.
    /k : Carries out the command specified by string and continues.

 

Process.Start("cmd", "/r ipconfig");

Upvotes: 1

Marco
Marco

Reputation: 57593

Take a look at this function (taken from here)

   public static string ExecuteCmd(string arguments)
    {
        // Create the Process Info object with the overloaded constructor
        // This takes in two parameters, the program to start and the
        // command line arguments.
        // The arguments parm is prefixed with "@" to eliminate the need
        // to escape special characters (i.e. backslashes) in the
        // arguments string and has "/C" prior to the command to tell
        // the process to execute the command quickly without feedback.
        ProcessStartInfo _info =
            new ProcessStartInfo("cmd", @"/C " + arguments);

        // The following commands are needed to redirect the
        // standard output.  This means that it will be redirected
        // to the Process.StandardOutput StreamReader.
        _info.RedirectStandardOutput = true;

        // Set UseShellExecute to false.  This tells the process to run
        // as a child of the invoking program, instead of on its own.
        // This allows us to intercept and redirect the standard output.
        _info.UseShellExecute = false;

        // Set CreateNoWindow to true, to supress the creation of
        // a new window
        _info.CreateNoWindow = true;

        // Create a process, assign its ProcessStartInfo and start it
        Process _p = new Process();
        _p.StartInfo = _info;
        _p.Start();

        // Capture the results in a string
        string _processResults = _p.StandardOutput.ReadToEnd();

        // Close the process to release system resources
        _p.Close();

        // Return the output stream to the caller
        return _processResults;
    }

Upvotes: 5

Related Questions