1

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();
4
  • 3
    Why can't you run "love" directly instead of running "cmd.exe?" Commented Aug 11, 2011 at 22:18
  • 1
    @Jacob: Probably because he couldn't use @"cd %ProgramFiles(x86)%\LOVE\" to change the working directory in that case. Commented Aug 11, 2011 at 22:20
  • 1
    In the StartInfo object, you can set its WorkingDirectory property to achieve the same effect as executing a cd command. Commented Aug 11, 2011 at 22:23
  • BTW the console window closes after 1 second, because the code doesn't wait for the process to finish but closes it immediately. Commented Aug 11, 2011 at 22:26

3 Answers 3

4

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 '&&'.

Sign up to request clarification or add additional context in comments.

1 Comment

In the end, this solution is the best. I was so keen on the cmd, that i forgot a simple starting parameter can do it too. Process cmd = new Process(); cmd.StartInfo.FileName = @"C:\Program Files (x86)\LOVE\love.exe"; cmd.StartInfo.Arguments = "\""+fldBrowDiag.SelectedPath.ToString()+@"\"+lsb_projects.SelectedItem.ToString()+"\""; cmd.Start();
2

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" });

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.