Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble launching program with System.Diagnostics.Process.Start()

Tags:

c#

.net

What should be a trival matter has now gobbled up an hour of my time! >__<

I want to start an executable with a xml path as an argument. (I've added the directory in which this program resides to my system path.) Seems simple enough. My first approach was using the static Process.Start() method as such:

Process.Start(@"MyExecutable.exe", "C:\\My Doc\\SomeDirectory\\MyXMLPath.xml");

The process does start, but like half a second later it dies. So, I'm like ooookaay, maybe the executable doesn't like the argument I'm giving it? Just for giggles, I created a shortcut to the executable and added the xml file path as one of it's arguments. The program starts and runs as expected. Not sure why this works, I decided to test my luck on the command line as well:

C:\MyExecutable.exe "C:\My Docs\SomeDirectory\MyXMLPath.xml"

No problem starting this way too.

Now, at this point I started grasping for straws and decided to create an instance of the Process class:

Process proc = new Process();
proc.StartInfo.FileName = @"MyExecutable.exe";
proc.StartInfo.Arguments = "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml";
proc.Start();

Needless to say this didn't help at all. >.<

So frusterated, I decided test my luck commenting out one line:

//proc.StartInfo.Arguments = "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml";

And the process starts. Not with its necessary arguments, but it starts. So, the question is, why is the program not accepting the path I give it when I try to launch it through the Process class? If only it left me a message when the program died. :(

Any thoughts? Much appreciated!

like image 719
Ashley Grenon Avatar asked Feb 26 '23 11:02

Ashley Grenon


1 Answers

The problem might be because of the spaces in the file path. If you think about it in how you made the DOS call you had to put quotes around the path. But the way you are calling you are not. So try adding single quotes around the path. That should take care of it. If you think about it from the standpoint of how that would look rendered as a command line then it makes more sense why you need to do that.

Process proc = new Process();
proc.StartInfo.FileName = @"MyExecutable.exe";
proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\"";
proc.Start();
like image 126
spinon Avatar answered Apr 08 '23 16:04

spinon