[OTP] running DOS commands using C#

Hello there......

i am developing an application and as a part of it i want to run DOS commands through my code. i am using C#. in the MSDN library i saw a mention of "System.Diagnostics.Process.Start" and using cmd.exe. but i am relly not sure how to use this, moreover there is no System.Diagnostics.Process? do i need any references?

please if someone could help....

thanks a lot.....

naaz

[587 byte] By [naaz911] at [2008-2-8]
# 1

Process.start is a .net framework method and is available to all .net languages.

However as your using c# I would recommend asking the question in the C# Forums rather than the VB.Net ones

C# Forums

http://forums.microsoft.com/MSDN/default.aspx?forumgroupid=9&siteid=1

To use process start uses the system.diagnostics namespace which is in the default imports. I'm not sure what is in the default C# ones.

spotty at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 2

naaz,
It's strange you don't see System.Diagnostics.Process: it's in System.dll and I guess it's imported by default.

The simplest way to run a command is:

System.Diagnostics.Process.Start ("cmd", @"/c copy c:\myfolder\*.* c:\mybackup");

You can get creative using System.Diagnostics.ProcessStartInfo... for instance you can have your command run without showing a window and capturing the output of the command to process it as you prefer (for instance, displaying it in your console). A simple example of this follows:

// create the ProcessStartInfo using "cmd" as the program to be run, and "/c dir" as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
// To have more information, start cmd and type help cmd.
System.Diagnostics.ProcessStartInfo sinf =
new System.Diagnostics.ProcessStartInfo ("cmd", "/c dir");
// The following commands are needed to redirect the standard output. This means that it will be redirected to the Process.StandardOutput StreamReader.
sinf.RedirectStandardOutput =
true;
sinf.UseShellExecute =
false;
// Do not create that ugly black window, please...
sinf.CreateNoWindow =
true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process p =
new System.Diagnostics.Process ();
p.StartInfo = sinf;
p.Start (); // well, we should check the return value here...
// We can now capture the output into a string...

string res = p.StandardOutput.ReadToEnd ();
// And do whatever we want with that.
Console.WriteLine (res);

HTH
--mc

MarioCossi at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 3

Hey dude,

Thanks for the code. This helped me to solve my problem.

Keep it up man.

Geethanga at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 4

thanks !

It helped me.

Jitendra.net at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 5
this works great but is there any way to do it without creating a process?
laslous at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 6
I can only think of creating a batch file and running that, although that too would require creating a process.
psymen at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 7

Anyone please advice how to execute other commands like "cd","cd ..","fc /?" similarly in cmd using c#.

vinnu at 2007-9-10 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...