Click here to Skip to main content
15,900,646 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to run a legacy app that is run from a cmd window using the Process class.

C#
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = "/C \"C:\\UTDSys\\UTD2TCM.exe –r " + Parameters.Candidate + "\"";
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false;  

try 
{ // Start the process with the info we specified.     
  // Call WaitForExit and then the using statement will close.     
  using (Process exeProcess = Process.Start(startInfo))     
  {         
    exeProcess.WaitForExit();     
  }  
} 
catch (Exception e) 
{     
  string sMsg = "Error copying the files to " + Parameters.FullPath + ".";
  HandleErrorMsg(e, sMsg);
  return; 
}

The process My2Com.exe should run in the background, however, I consistantly get the message that a file, used when run from the cmd line with different flags, is missing. If I run the command as indicated in a cmd window, C:\MySys\My2Com.exe –r FullyQualPath, it works as expected. I have tried several different ways to set up the Process class without success.

Any suggestions would be appreciated.

Thank you,

Tom R.
Posted
Updated 15-Aug-12 9:11am
v2
Comments
[no name] 15-Aug-12 14:44pm    
You have a missing set of double quotes in your string. \"C:\\MySys\\My2Com.exe\", I think
[no name] 15-Aug-12 14:45pm    
Or \"C:\\MySys\\My2Com.exe –r " + Parameters.FullPath + "\""
CliffRat 15-Aug-12 15:12pm    
Thank you, Wes, for spotting that. It is actually correct in the program and I have corrected it here.
[no name] 15-Aug-12 15:53pm    
Back again. Any particular reason that you are running cmd.exe and not your application directly?
CliffRat 15-Aug-12 16:41pm    
This was just one of the ways I attempted to get this to work. I started out with...
startInfo.FileName = "c:\MySys\My2Com.exe";
startInfo.Arguments = "–r " + Parameters.FilePath;
and got the same results.

You don't need "CMD" to start your application. "CMD" is another application, nothing more, it does not add anything to your functionality. Simply use a static method System.Diagnostics.Process.Start:
http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx[^].

In your case:
C#
System.Diagnostics.Process.Start("C:\\UTDSys\\UTD2TCM.exe", string.Format("-r {0}", Parameters.Candidate);


Now, please understand that there is no cases where the hard-coded path names like "C:\UTDSys\UTD2TCM.exe" can be useful, unless you want to develop an application which can work only on your development computer. This file may not be placed in this directory, the directory might not exist, and even the presence of the volume "C:" is not a must (on one of my computers, there is no such drive). The file name is always calculated based on user input, some configuration files, assembly location, "special directories" (related to users' accounts, those per user or for all users), etc.

—SA
 
Share this answer
 
Comments
CliffRat 15-Aug-12 16:42pm    
See my last response to Wes. I used, essentially, the same string info you indicate.
Sergey Alexandrovich Kryukov 15-Aug-12 17:23pm    
Maybe, but this is not a problem. Get rid of "CMD" and get rid of hard-coding.

Didn't I answer your question? Then please accept it formally (green button) -- thanks.
--SA
Abdul Quader Mamun 15-Aug-12 21:39pm    
helpful Answer!
Sergey Alexandrovich Kryukov 15-Aug-12 21:41pm    
Thank you, Abdul.
--SA
CliffRat 16-Aug-12 8:55am    
I understand all the points you made and they have been considered in 'the real world'. My tests have included variations without using CMD. Credit were due, the solution appears to be how the arguments string was contructed. I have substituted yours above (string.Format("-r {0}", Parameters.FilePath)) and that seems to have fixed it. Also, "-r " + Parameters.FilePath.ToString() works.

Thank you for pointing me in the right direction.
The fix involves how the arguments string is constructed:
C#
startInfo.FileName = "C:\\UTDSys\\UTD2TCM.exe";
startInfo.Arguments = "-r " + Parameters.FilePath.ToString();

or
C#
startInfo.FileName = "C:\\UTDSys\\UTD2TCM.exe";
startInfo.Arguments = string.Format("-r {0}", Parameters.FilePath);


Thanks to everyone for your help.
 
Share this answer
 

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