문제

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

도움이 되었습니까?

해결책

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

다른 팁

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top