In some cases, the command prompt command may be executed directly from the C#application. A common case is that automation usually requires manually input in the command window.
For this reason, the
class in the name space provides the necessary functions. By calling the method of System.Diagnostics
, arbitrary commands can be executed on the operating system. Process
Process
For example, to execute the command "Start
" (embedded the RAR file into the JPG image), you can use the following code fragment:
copy /b Image1.jpg Archive.rar Image2.jpg
To hide the command window during the execution period, please modify the
<code class="language-csharp">string strCmdText; strCmdText = "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe", strCmdText);</code>
ProcessStartInfo
Please note that the parameter strings must start with "
<code class="language-csharp">System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; process.StartInfo = startInfo; process.Start();</code>
The above is the detailed content of How Can I Run Command Prompt Commands from a C# Application?. For more information, please follow other related articles on the PHP Chinese website!