Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 462
Aloehart Avatar asked Feb 11 '26 04:02

Aloehart


2 Answers

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.

like image 133
Patrick Hofman Avatar answered Feb 13 '26 16:02

Patrick Hofman


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);
like image 40
Matt Avatar answered Feb 13 '26 18:02

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!