Redirecting Standard Input/Output using the Process Class (轉)
Code: corner.com/References/System.Diagnostic.Procesde.zip">System.Diagnostic.ProcessCode.zip
When a program starts, a new process (a program in execution) is created by the operating system, and this process is ntified by its process Id and also a name. A set of counters are set attached to this process by the in order to have some statistics about it.
In, the process component is used to start a new process or even kill or suspend an existing one. Also we can get some statistics about the process already running through the following properties:.
NonpagedSystemMemorySize:
PagedMemorySize:
PagedSystemMemorySize:
PeakPagedMemorySize:
PeakVirtualMemorySize:
PeakWorkingSet
PriorityClass
PrivateMemorySize
PrivilegedProcessorTime
The process component offers static and instance methods. Some very important static methods are:
EnterDeMode: Put the process in debugging for interaction with the OS.
GetCurrentProcess: Returns the current running process.
GetProcessById: Returns a process instance of an existing process receiving as parameter the process id.
GetProcesses: Returns an array of Process instances of the currently running process in the host machine .
Start: Starts a new process.
The Process Class Methods:
Close: Frees the resources associated with an instantiated process component.
Kill: Tenates a running process
CloseMainWindow: Close a process which a main by sending a message to its main window.
The Process Class Properties:
Responding: Gets a value about the current status of a process: true for running false for not Responding.
StandardError: Gets a file descriptor for the standard error in order to be able to redirect to a normal Stream Reader and read as a file..
StandardInput: Gets the standard input of the process as file descriptor in order to be able to redirect and write to it as if writing to a StremWriter.
StandardOutput; Similir as the Standard error but used to read the standard output of the process.
StartInfo: This property has to be set with a ProcessStartInfo instance to pass initialization parameters to the process, before it starts. Altering this process after starting a process has no effect.
The list of properties and methods above show is not exhaustive, you can find some other ones through the help online.
In this example I'll try to illustrate some of the power of this component by starting a normal process (windows app) or also by redirecting the standard error, output and input of a DOS app. Also, you will see how to execute DOS commands by using the process component. You can also extend the example by
reading some of the counters associated to a process and showing them in a window, like the Task Manager app. The let's see the example and some important details about it:
First of all we need to instantiate a ProcessStartInfo class passing as a constructor param the name of the app that we want to launch and to set some parameters to be passed to the Process instances (p):
ProcessStartInfo psI = new ProcessStartInfo("cmd");
The property psI.UseExecute was set as false, to be able to redirect the StandardInput, etc. After that, the properties
psI.RedirectStandardInput
psI.RedirectStandardOutput
psI.RedirectStandardError
...are set to true.
To avoid the cmd annoying window we set as true the property psI.CreateNoWindow so no window will be created for the cmd app.Finally we set the p.StartInfo property to the instance of ProcessStartInfo
that we have just created and voila , starts the process.
To be able to capture the p.StandardInput, p.StandardOutput and p.StandardError we get the File Descriptors (StreamReaders and StreamWriter classes) to read the StandardOutput and StandardError and to write to the StandardInput. The read and write operations are performed as we do it in a normal file. (Hmm, does this way of working with the IO remind you the world?) The process cmd is closed when we close the p.StandardInput file descriptor.
At the very end we read the p.StandardOutput and p.StandardError file descriptors into the text box. See the complete code of the method that implements the whole stuff.
{
Process p = new Process();
StreamWriter sw;
StreamReader sr;
StreamReader err;
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;
sw.AutoFlush = true;
if (tbComm.Text != "")
sw.WriteLine(tbComm.Text);
else
//execute default command
sw.WriteLine("dir ");
sw.Close();
textBox1.Text = sr.ReadToEnd();
textBox1.Text += err.ReadToEnd();
}
About the Author:Edwin Lima is a software engineer working for NEC Computers International BV in the Netherlands. Most of his time he's busy with data integration tasks and in his free time he plays around with and .NET related stuff (You can contact me at: edwinlima@hot.com)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993878/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- angular input和outputAngular
- bash : input/output errorError
- Input/output error [ linux ]ErrorLinux
- mount error(5): Input/output errorError
- logstash http input output pluginHTTPPlugin
- mount.nfs: Input/output errorNFSError
- input delay和output delay講解
- MapReduce--Input與Output規則
- RAID 磁碟故障input/output errorAIError
- A+B for Input-Output Practice (IV) (sdut oj)
- 1128rman Input or output Memory Buffers
- Oracle Display the process of the procedure ( oracle pipe , dbms_output , prompt )Oracle
- Fatal error in launcher: Unable to create process using '"'Error
- [Symfony Component Process Exception RuntimeException] The Process class relies on proc_open, whichException
- HDOJ 1094 A+B for Input-Output Practice (VI)
- [20171128]rman Input or output Memory Buffers.txt
- Logstash學習記錄--logstash input output filter 外掛總結Filter
- onnx 增刪改查,修改節點,刪除節點,修改input,output
- For oracle databases, if the top showing the oracle database, then oracle process is using the top cOracleDatabase
- linux 執行shell 報錯stty: standard input: Inappropriate ioctl for device解決LinuxAPPdev
- Scan Your Truck Using Nexiq Adapter: Simplifying Your Diagnostic ProcessAPT
- Redirecting to /bin/systemctl stop iptables.service
- 《轉》Java Process應用之惑Java
- C++ Standard Library擴充建議... (轉)C++
- ElasticSearch7.3學習(三十二)----logstash三大外掛(input、filter、output)及其綜合示例ElasticsearchFilter
- 1001. 溫度轉換 (Standard IO)
- New I/0 Functionality for JavaTM 2 Standard Edition 1.4 (轉)FunctionJava
- Structured OutputStruct
- Standard Database AuditingDatabase
- Using a Virtual CListView with a Dynaset (轉)View
- chain RH-Firewall-1-INPUT(轉)AI
- requirement for output typeUIREM
- Process Monitor Process (PMON) (121)
- Using WebLogic Server With Oracle RAC(轉)WebServerOracle
- C++ coding standardC++
- .NET Standard是什麼
- Go Standard library - flagGo
- [轉] Input的高階用法11例