Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute operating system commands

Status
Not open for further replies.

skywalker74

Programmer
Jul 25, 2003
7
0
0
IT
Hi everyone, how can i launch dos-like commands via c#??
for example in java i use Runtime.getRuntime().execute("command");



thanks...
 
The best way to do this in C# in my opinion is using System.Diagnostics.Process, and you can use, for example

Code:
ArrayList ProcessList = new ArrayList();
ProcessList.Add(new Process());
  ((Process)ProcessList[ProcessList.Count-1]).StartInfo.FileName = "myexecutable.exe";
  ((Process)ProcessList[ProcessList.Count- 1]).StartInfo.RedirectStandardOutput = true;
  ((Process)ProcessList[ProcessList.Count-1]).StartInfo.CreateNoWindow = true;
  ((Process)ProcessList[ProcessList.Count-1]).EnableRaisingEvents = true;
  ((Process)ProcessList[ProcessList.Count-1]).StartInfo.UseShellExecute = false;
  ((Process)ProcessList[ProcessList.Count-1]).Exited += new EventHandler(Process_exited);
  ((Process)ProcessList[ProcessList.Count-1]).Start();

//This is only if you want a process exited handler, remember, you _have_ to set .EnableRaisingEvents to true in order for this to work
private void Process_exited(object sender, System.EventArgs ea) 
{
  //Any processesing you want here
}

Thats just an example of psuedo-threading per-se storing the processes in an ArrayList, but you should be able to get the gyst of it from that.

If you don't want to store it in an ArrayList (which I doubt you do, go ahead and just remove the cast as (Process) and just use YourProcessName.StartInfo.etc etc)

Hope this helps,
Regards,
John
 
If I had even read slightly, if you want to use dos commands just pipe them from cmd, like:

Code:
cmd | dir > my_test.txt | exit

Or something to that extent, just run the process with arguments.

Regards,
John
 
Ok johnrbradley, it seems that your code work fine, but if i try inserting the command posted after: cmd | dir > my_test.txt | exit

it gives the following error:
The filename, directory name, or volume label syntax is incorrect
this is my code overall:

ArrayList ProcessList = new ArrayList();
ProcessList.Add(new Process());
((Process)ProcessList[ProcessList.Count-1]).StartInfo.FileName = "C:\\WINNT\\system32\\cmd.exe | dir > my_test.txt | exit";
((Process)ProcessList[ProcessList.Count- 1]).StartInfo.RedirectStandardOutput = true;
((Process)ProcessList[ProcessList.Count-1]).StartInfo.CreateNoWindow = true;
((Process)ProcessList[ProcessList.Count-1]).EnableRaisingEvents = true;
((Process)ProcessList[ProcessList.Count-1]).StartInfo.UseShellExecute = false;
((Process)ProcessList[ProcessList.Count-1]).Exited += new EventHandler(Process_exited);
((Process)ProcessList[ProcessList.Count-1]).Start();
 
Ah, simple problem to correct, forgive me for forgetting to put it in the example:
Code:
((Process)ProcessList[ProcessList.Count-1]).StartInfo.Arguments ="arg1 arg2 arg3";

Regards,
John
 
{PS} And there is really no reason to even pipe to a file in the first place since you can redirect the standard output and error to any variable/component that you want. So if you were gonna use that method to retrieve data from the application that you ran, I suggest
((Process)ProcessList[ProcessList.Count-1]).StartInfo.RedirectStandardOutput = true;

Then with the ExitHandler you would:

String stdout = ((Process)ProcessList[inc]).StandardOutput.ReadToEnd();

[or something]

Regards,
John
 
it doesn't work yet. Where should i put exactly, that line of code?
 
Code:
((Process)ProcessList[ProcessList.Count-1]).StartInfo.FileName = @"C:\WINNT\system32\cmd.exe";
((Process)ProcessList[ProcessList.Count-1]).StartInfo.Arguments = "| dir";

This _should_ work, though its off the top of my head, good luck.

Regards,
John

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top