简体   繁体   中英

Process.Start and Process.StartInfo not passing arguments properly

The short story is I'm writing a front-end for the emulator MAME as a study in WPF and C#.

The GUI is set, it's reading cfg's properly, everything is fine except for actually launching MAME.

From the command line (Windows 7) I can type the following and have the emulator launch properly.

c:\MAME\Emulator\mame.exe mslug.zip

That launches the emulator with no issue exactly as designed. However I have tried all of the following.

Process Mame = new Process(emulatorPath);
Mame.StartInfo.Arguments = romSelected;
Mame.Start();

I tried the above with both the variable and putting Mame.StartInfo.Arguments = "mslug.zip"

ProcessStartInfo Mame = new ProcessStartInfo(emulatorPath);
Mame.Arguments = romSelected;
Process.Start(Mame);

I tried this also with both the variable and putting "mslug.zip" in it's place.

Finally I tried the following.

Process.Start(@"c:\Mame\emulator\mame.exe", "mslug.zip");

And it acts the same as the previous attempts.

If I don't try passing arguments to it, the program launches fine and just tells me there was no rom. Any of the above methods of passing arguments all resulted in a quick command prompt blip showing the same info it would show if it the rom's zip file was empty.

From what I've read about Process.Start and the like, what I've typed above should equate to opening the command line and entering the command I started this post with. But if that were the case then this should have worked with no issue. I'm not sure if I'm doing something wrong or if there is a better way to go about this.

Note: I also went through the Windows GUI and created a shortcut to mame.exe and edited it's properties to pass mslug.zip as an argument and it worked as well, so it doesn't require it to be done through the command line as far as I can tell.

As an asside, I have debug textboxes in the gui of the app that are updated with the variables used in my code to verify that the variables are correct.

Update:
I wanted to add that the program (for those not familiar) relies on the filename of the rom you try to launch. Meaning passing the argument mslug.zip causes the program to goto it's own rom directory (currently C:\mame\emulator\roms) and search for mslug.zip. I can run that command from any directory in my system and get the same result. I can also pass the path to the rom like

c:\mame\emulator\mame.exe c:\mame\emulator\roms\mslug.zip

That will also work regardless of where I run it. And I have tried that within my code, both by passing the paths as variables and by passing them like

string romSelected = @"c:\mame\emulator\roms\mslug.zip";

Both fail in the same fashion.

The code you use works fine. It does send the right argument to the program. No doubt.

Some things that could go wrong:

  • The file doesn't exist ;
  • The file doesn't exist in the current working directory , aka the place where mame is looking for it (you can try using an non-existing file name in the command line as a test. Is the error the same, then probably the file can't be found. Try to use the full path of the file );
  • Some permission problems, the error is obscured by the program.

As Sinatr suggested, you could set the current working directory using Directory.SetCurrentDirectory . Be aware that the current working directory of this program is influenced too, so you might want to consider what you are doing.

You'd better set the working directory of the process you start, using Process.WorkingDirectory .

Only assigning StartInfo.Arguments with the filename does not work. You must prepend an action like -File. This will generate 2 arguments for the receiving App (args[0]="-File", args[1]=filename).

Here's what worked for me:

ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
    FileName = appFilename,
    Arguments = "-File " + viewFilename, // space char after -File
};
Process.Start(startInfo);

Now I finally got this to work. The magic lies in the pre string to the args.

example:

/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.

described here: http://ss64.com/nt/cmd.html

The snippet below executes the DIR command using CMD.exe

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
// /K Run Command and then return to the CMD prompt.
startInfo.Arguments = "/K dir";
process.StartInfo = startInfo;
process.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM