Liam Schnell
Liam Schnell

Reputation: 482

Running cmd with C# and parameter

I have to compile a game like this

love "C:\testgame"

in the cmd. So I use this code, but it seems like the parameter is missinterpreted. Also, the console closes after a sec. But if I use Messagebox.Show I can see the command in the cmd is the same I manually use (and this works)

Process cmd = new Process();

        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.RedirectStandardInput =
        true;
        cmd.StartInfo.RedirectStandardOutput =
        true;
        cmd.StartInfo.CreateNoWindow = false;
        cmd.StartInfo.UseShellExecute = false;

        cmd.Start();
        cmd.StandardInput.Write(@"cd %ProgramFiles(x86)%\LOVE\");
        MessageBox.Show("love \""+fldBrowDiag.SelectedPath.ToString()+@"\"+lsb_projects.SelectedItem.ToString()+"\"");
        cmd.StandardInput.Close();
        cmd.Close();

Upvotes: 1

Views: 962

Answers (3)

Jacob
Jacob

Reputation: 78850

Try this to simplify things:

var process = Process.Start(
    new ProcessStartInfo(@"C:\Program Files (x86)\LOVE\love.exe", @"C:\game") {
        WorkingDirectory = @"C:\Program Files (x86)\LOVE" });

Upvotes: 2

Bob Vale
Bob Vale

Reputation: 18474

Why can't you just start cmd with the correct arguments to launch your process?

eg cmd /C love "c:\game" to close after finish or cmd /K love "c:\game to leave open after finish?

Upvotes: 0

Daniel Walker
Daniel Walker

Reputation: 770

First, the "cd" command you issue will probably fail because you don't have quotes around the argument. (that program files env variable will have spaces in it.)

Second, instead of writing to stdin directly, maybe consider using the "/c" switch that will instruct cmd.exe to execute the specified commands directly. You can separate the commands with '&&'.

Upvotes: 4

Related Questions