Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
4.86/5 (7 votes)
See more:
Hello everyone,

I have a program called Analyze.exe. It is normally ran from the command line like this:

c:\analyze.exe "C:\some path\filename.dat" "C:\some path\filename.out"



I want to run this program from some C# code, but it is failing. It appears to be failing because there are spaces in the filename being passed in. If anyone knows what I am doing wrong or has another idea how to accomplish this please point it out!
public void runProcess()
{
   
   string inputPath = "\"C:\\some path\\filename.dat\"";
   string outPutPath = "\"C:\\some path\\filename.out\"";

   System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
   pProcess.StartInfo.FileName = "c:\\analyze.exe"
   pProcess.StartInfo.Arguments = inputPath + " " + outputPath;
   pProcess.Start();
   pProcess.WaitForExit();
   pProcess.Close();
}


After the execution of the 5th line inside the method body --
If you were to poll the pProcess.StartInfo.Arguments property value, you see what you would expect. ( a string with Quote marks at the begining and ending of the string. )


Somewhere in the execution of the above code the Quote marks are being omitted from the Arguments causing problems down stream!


To test this I replaced the actual Analyze.exe with this program listed below.

class Program
{
   static void Main( string[] args )
   {
      for ( int i = 0; i < args.Length; i++ )
      {
         Console.WriteLine("args[i] = " + args[i] );
      }

      Console.ReadLine();
   }


Any help will be deeply appreciated,

Mike
Posted

Good question Mike.

This is how you can do this:

C#
string inputPath = "\"\\\"C:\\some path\\filename.dat\\\"\"";
string outPutPath = "\"\\\"C:\\some path\\filename.out\\\"\"";
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:42pm    
My 5. See also two notes in my Answer, can be useful.
--SA
mike1989 12-Apr-11 14:53pm    
Thank you,

Using your code the string has the begining and ending quotes displayed on the console screen in the test program.


Using the code from the above post DID NOT include the begining and end quotes displayed on the console screen in the test program. However, the total parameter count was correct in both cases being under test.

-Just a little detail that I noticed,
Thanks again,

Mike
Espen Harlinn 12-Apr-11 15:16pm    
Nicely spotted, my 5 :)
Toli Cuturicu 12-Apr-11 15:58pm    
It may better, for readibility sake, to use verbatim @ string syntax here... Just an idea.
ProEnggSoft 3-Mar-12 18:48pm    
I too of the same opinion.
Maybe, just side notes:

1) Do you know that you can re-direct output of the console application to any stream (not only the file, but, say, some stream designed to read and show results in UI or something like that)? See this sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^]. You can redirect both stdout and stderr, see System.Diagnostics.Process.StandardOutput, System.Diagnostics.Process.StandardInput, see also System.Diagnostics.Process.StandardInput.

2) If you use Process.WaitForExit() (you may or may not need it), you should understand this is blocking call, so you need to do it all in a separate thread. This is easy enough.

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 12-Apr-11 15:17pm    
It's useful to know about, 5ed!
Sergey Alexandrovich Kryukov 12-Apr-11 16:09pm    
Thank you, Espen.
--SA
Albin Abel 12-Apr-11 15:41pm    
my 5
Sergey Alexandrovich Kryukov 12-Apr-11 16:10pm    
Thank you, Albin.
--SA
ProEnggSoft 3-Mar-12 18:46pm    
Good info. My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900