Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body
i want to open cmd via c# and then want to change the directory in cmd and execute some .exe file.
MIDL
Process p = new Process();
        string result = "";

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = tru
Posted
Comments
OriginalGriff 1-May-11 3:00am    
Why? What are you trying to achieve?
You will probably find it is easier to create a batch file, and execute that instead...

You do not what to do that.

Play around with Process class[^] and StartInfo[^]. There you can specify what startup directory, parameter and what .exe file you what to startup. No need to use CMD for that.

An example of how to use Process.Start with arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");

ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 1-May-11 17:07pm    
Sure, my 5.
--SA
Kim Togo 2-May-11 2:22am    
Thanks SA
Sergey Alexandrovich Kryukov 1-May-11 17:11pm    
However, I do not recommend it with IExplorer. There is much better way.

Please see my answer.
--SA
Kim Togo 2-May-11 2:24am    
I forgot to write, that the use of IExplore was just an example. OP have not written about IExplore. But executing .EXE files in general.
RaviRanjanKr 1-May-11 23:36pm    
Good, My vote of 5 :)
If you just want to execute an exe file and you want to open cmd for the same so i would suggest directly executing the exe. Here is the code.

System.Diagnostics.Process.Start("Path","Arguments");
 
Share this answer
 
Comments
haseebsvirgo 1-May-11 4:23am    
how to give input to particular exe file
Rahul_Patel 1-May-11 4:27am    
If your exe file is taking command line arguments than you can supply it as a string in the second argument just like we provide argument in cmd.
Sergey Alexandrovich Kryukov 1-May-11 17:11pm    
My 5. However, I do not recommend it with IExplorer. There is much better way.

Please see my answer.
--SA
Running another process should be the last resort.
I do not recommend doing this with IExplore, because you won't have a way of communicating with this process when it's running.

There is much better way: use the Web browser control System.Windows.Forms.WebBrowser in you UI application:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Kim Togo 2-May-11 2:27am    
OP have not written or asked about IExplore. I took it just as an example of how to use Process.Start method.
But you are right about the use of WebBrowser UI control :-)
Sergey Alexandrovich Kryukov 2-May-11 3:43am    
I see, thank you for this note. Pity :-)
--SA

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