views:

79

answers:

2

I would like to mimic the Run command in Windows in my program. In other words, I would like to give the user the ability to "run" an arbitrary piece of text exactly as would happen if they typed it into the run box.

While System.Diagnostics.Process.Start() gets me close, I can't seem to get certain things like environment variables such as %AppData% working. I just keep getting the message "Windows cannot find '%AppData%'..."

+4  A: 

You can use the Environment.ExpandEnvironmentVariables method to turn %AppData% into whatever it actually corresponds to.

Dean Harding
Nice! Thanks for that.
chaiguy
+1  A: 

Depending on what you're trying to do, you could also call CMD.EXE, which will expand your environment variables automatically. The example below will do a DIR of your %appdata% folder, and redirect the stdOut to the debug:

        StreamReader stdOut;

        Process proc1 = new Process();
        ProcessStartInfo psi = new ProcessStartInfo("CMD.EXE", "/C dir %appdata%");
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;
        proc1.StartInfo = psi;
        proc1.Start();
        stdOut = proc1.StandardOutput;

        System.Diagnostics.Debug.Write(stdOut.ReadToEnd());
Robaticus
Thanks, but I don't want to limit commands to the command window.
chaiguy