Click here to Skip to main content
15,173,910 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);
   
v5
Comments
   
Sure, my 5.
--SA
Kim Togo 2-May-11 2:22am
   
Thanks SA
   
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 :)
Kim Togo 2-May-11 2:24am
   
Thanks Ravi.
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");
   
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.
   
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
   
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 :-)
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month



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