Reputation: 9565
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!
Upvotes: 4
Views: 3456
Reputation: 45799
Your arguments contain a space:
proc.StartInfo.Arguments = "C:\\My Docs\\SomeDirectory\\MyXMLPath.xml";
This means that the program you're executing is receiving the following as its arguments:
C:\My
Docs\SomeDirectory\MyXMLPath.xml
What you need to do is wrap it in double quotes (not single as suggested in another answer), thusly:
proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\"";
which means that the program you're executing will now receive only one argument:
"C:\My Docs\SomeDirectory\MyXMLPath.xml"
Upvotes: 3
Reputation: 10857
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();
Upvotes: 4
Reputation: 49965
You should put in a handler for the Exited
event of the process:
Process p = new Process();
p.Exited += new EventHandler(MyProcess_Exited);
and then check the ExitCode
inside the handler:
private void MyProcess_Exited(object sender, EventArgs e)
{
Process p = sender as Process;
if (p.ExitCode == someValue)......
}
in the absense of any other info i would say that the privileges of the started process are insufficient to read or access the xml file.
Upvotes: 1