Pergunta

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%'..."

Foi útil?

Solução

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

Outras dicas

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());
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top