Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to commands. My application need to run another .exe file with the help of command window (cmd.exe) in C# Winforms;


Below peace of code the one of the folder having(My App 4) a space. Due to this space the command prompt not execute app.exe.

What I have tried:

I tried:-

var process= new ProcessStartInfo();
string Path = @"C:\PROGRA~1\My App 4\Compare\app.exe";
process.UseShellExecute` = true;
proccess.WorkingDirectory = @"C:\Windows\System32";
process.Filename = @"C:\Windows\System32\cmd.exe";
process.Arguments = "/c " + Path;
proccess.WindowStyle = ProcessWindowStyle.Hidden;
process.Start(process);
Posted
Updated 26-Oct-17 1:57am
Comments
Graeme_Grant 26-Oct-17 7:31am    
What is your question?
Rahul s menon 26-Oct-17 7:45am    
how to run this app.exe from the path mentioned above with command window.

The path you're trying to execute has to be enclosed in quotes in your command line.

The command line you appear to be trying to execute is:
cmd /c C:\PROGRA~1\My App 4\Compare\app.exe


What you need to be executing is:
cmd /c "C:\Program Files\May App 4\Compare\app.exe"

Any path that has spaces in it must be enclosed in quotes.
So, the quick'n'dirty method of setting your Path would be
string Path = "\"C:\\Program Files\\My App 4\\Compare\app.exe\"";
 
Share this answer
 
Comments
Rahul s menon 26-Oct-17 8:04am    
For Progarm Files we using PROGRA~1. Like this way any solution to implement "My App 4"?
Dave Kreskowiak 26-Oct-17 8:16am    
Yes, I already told you how. If there is a space anywhere in the path, the path MUST be enclosed in quotes.
Dave Kreskowiak 26-Oct-17 8:17am    
For Progarm Files we using PROGRA~1
Why? There's no point to it today.
You would need to put Path in double-quotes because it's used as an argument, not as the FileName here. Same thing as when you would type it into a command console manually.

Are you sure you actually don't want to execute your app directly, without the detour via cmd.exe? Like this:
C#
Process.Start(@"C:\PROGRA~1\My App 4\Compare\app.exe");
 
Share this answer
 
v2
Comments
Rahul s menon 26-Oct-17 7:51am    
Thanks for your solution. I want to specify the path to a string variable as mentioned in the peace of code

Process.Start(@"C:\PROGRA~1\My App 4\Compare\app.exe"); only issue am facing is "My App 4" this folder names having spaces. Due to this space not able to run app.exe. for Progarm Files we using PROGRA~1. Like this way any solution to implement "My App 4"
Sascha Lefèvre 26-Oct-17 10:00am    
Process.Start(@"C:\PROGRA~1\My App 4\Compare\app.exe"); will work regardless of spaces. Spaces are only a problem in your code because you use the path as an argument. Enclose it in double quotes, as I said and as Dave showed below.

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