Alarm Launcher with System.Diagnostics.Process.Start ()

What should be a tricky question has now consumed an hour of my time! > __ <

I want to run an executable using the xml path as an argument. (I added the directory in which this program is on my system path.) It seems pretty simple. My first approach was to use the static Process.Start () method as such:

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

The process begins, but after half a second he dies. So, I look like ooookaay, maybe the executable doesn't like the argument I give it? For a giggle, I created a shortcut for the executable and added the path to the xml file as one of its arguments. The program starts and starts as expected. Not sure why this works, I decided to check my luck on the command line too:

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

No problem with this path.

Now, at that moment, I started grabbing at a straw 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 did not help at all ..> & L;

So exhausted, I decided to test my luck by commenting on one line:

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

And the process begins. Not with his necessary arguments, but he begins. So, the question is why the program does not accept the path that I give it when I try to run it through the Process class? If he had left me a message when the program died. :(

Any thoughts? Very grateful!

+4
source share
3 answers

The problem may be due to spaces in the file path. If you think about it in the way you made the DOS call, you had to put quotes around the path. But the way you call is wrong. So try adding single quotes along the way. That should take care of this. If you think about it in terms of how it would look like a command line, then it will be more clear why you need to do this.

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

Your arguments contain a space:

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

This means that the program you are executing receives the following arguments:

  • C:\My
  • Docs\SomeDirectory\MyXMLPath.xml

What you need to do is wrap it in double quotes (not single quotes, as suggested in another answer), like this:

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

which means that the program you are running will now receive only one argument:

  • "C:\My Docs\SomeDirectory\MyXMLPath.xml"
+3
source

You must place a handler for the Exited process event:

  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 absence of any other information, I would say that the privileges of the running process are not enough to read or access the XML file.

+1
source

All Articles