C # calling DOS command
I want to make an assembly language editor, but I can't get the running information when I call Masm.exe. The code is as follows:
Process P = new process ();
P.startinfo. WorkingDirectory = "e:\\";
p.StartInfo.FileName = "Masm.exe";
P.startinfo.arguments= "TEST.ASM;";
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardoutput = true;
P.start ();
String output = P.standardoutput.readtoend ();
p.WaitForExit ();
Textbox1.text= "AAA";
TextBox1.Text =output;
TextBox1 no problem, show the other can, but the display is not out of the runtime screen display information, just a blank.
Also, when I run CMD into DOS under the system, run MASM test.asm; Sometimes there is no display, sometimes there is compilation information, do not know why. This is also the case with running other executables. I wonder if there is any connection with the above question?
The command does not have output.
Process P = new process ();
P.startinfo. WorkingDirectory = "c:\\";
p.StartInfo.FileName = "Ping.exe";
P.startinfo.arguments= "www.sina.com.cn";
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardoutput = true;
P.startinfo.createnowindow = true;
P.start ();
String output = P.standardoutput.readtoend ();
p.WaitForExit ();
MessageBox.Show (output);
How to hide the Dos interface in C # by running DOS commands
One. NET:
p = new Process ();
p.StartInfo.FileName = "cmd.exe";
This is the key point without the shell boot/redirect input/redirect output/no display window
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardinput = true;
P.startinfo.redirectstandardoutput = true;
P.startinfo.createnowindow = true;
P.start ();
P.standardinput.writeline ("ping 127.0.0.1");//enter command to Cmd.exe
P.standardinput.writeline ("Exit");
p.WaitForExit (60000);
string s = P.standardoutput.readtoend ();//Get Cmd.exe output
P.close ();
?
Second, Win32:
Startupinfo si;//Here's too much to say, check MSDN yourself.
SI.CB = sizeof (SI);
CreateProcess ("cmd.exe", NULL, NULL, NULL, FALSE, create_no_window, NULL, NULL, &SI, NULL);
The Process class in C # can easily invoke external programs, so we can call the Cmd.exe program
Add the parameter "/C" + the command to execute to execute a DOS command
(/C indicates that the cmd.exe/k parameter is closed after the command specified by the execution parameter is not closed cmd.exe)
1 private string Runcmd (String command)
2 {
3//In fact, a process type that inspires a unique process
4 Process P = new process ();
5
6//process has a startinfo, this is processstartinfo, including some of the attributes and methods, and we use several of his attributes:
7
8 p.StartInfo.FileName = "cmd.exe"; Set the program name
9 p.startinfo.arguments = "/C" + command; To set the program to perform the parameters
P.startinfo.useshellexecute = false; Shutting down the use of the shell
One p.startinfo.redirectstandardinput = true; REDIRECT Standard input
P.startinfo.redirectstandardoutput = true; REDIRECT The standard output
P.startinfo.redirectstandarderror = true; REDIRECT Error output
P.startinfo.createnowindow = true; Setup does not display windows
15
P.start (); Activate
17
//p.standardinput.writeline (command); You can also enter commands to execute in this way
//p.standardinput.writeline ("Exit"); But remember to add exit or the next line of code
20
return P.standardoutput.readtoend (); From the output stream to get the results of the command.