如何执行一条命令在C#里面。Process
Introduction
It is normal practice to open the Windows command prompt and execute commands. The command when executed shows the result onto the screen. There are many commands that we execute daily such as dir, find, etc. A situation may arise when you want to execute a (shell) command from the C# application.
Don't worry!!! Here is the code to do so…
Using the Code
The code given below creates a process i.e. a command process and then invokes the command that we want to execute. The result of the command is stored in a string
variable, which can then be used for further reference. The command execution can happen in two ways, synchronously and asynchronously. In the asynchronous command execution, we just invoke the command execution using a thread that runs independently. The code has enough comments, hence making it self-explanatory.
Below is the code to execute the command synchronously:
/// <span class="code-SummaryComment"><summary></span>
/// Executes a shell command synchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command</param></span>
/// <span class="code-SummaryComment"><returns>string, as output of the command.</returns></span>
public void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); // The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
The above code invokes the cmd
process specifying the command to be executed. The optionprocStartInfo.RedirectStandardOutput
is set to true
, since we want the output to be redirected to theStreamReader
. The procStartInfo.CreateNoWindow
property is set to true
, as we don't want the standard black window to appear. This will execute the command silently.
Below is the code to execute the command asynchronously:
/// <span class="code-SummaryComment"><summary></span>
/// Execute the command Asynchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command.</param></span>
public void ExecuteCommandAsync(string command)
{
try
{
//Asynchronously start the Thread to process the Execute command request.
Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
//Make the thread as background thread.
objThread.IsBackground = true;
//Set the Priority of the thread.
objThread.Priority = ThreadPriority.AboveNormal;
//Start the thread.
objThread.Start(command);
}
catch (ThreadStartException objException)
{
// Log the exception
}
catch (ThreadAbortException objException)
{
// Log the exception
}
catch (Exception objException)
{
// Log the exception
}
}
If we observe carefully, the asynchronous execution of the command actually invokes the synchronous command execution method using a thread. The thread runs in the background making the command execution asynchronous in nature.
In the above execution sample, we find that there are two result sets of the command "dir
". The first one appears immediately after the command and the second appears after the "Done!" statement. In this case, the first one is the synchronous execution of the command, which happens immediately and the second is the asynchronous execution of the "dir
" command.
源地址:http://www.codeproject.com/Articles/25983/How-to-Execute-a-Command-in-C
如何执行一条命令在C#里面。Process的更多相关文章
- linux系统执行多条命令,linux系统执行复合命令
在操作linux系统的时候,你是否遇到过打开一个目录,然后查看一个文件里面的内容. 我们可以使用命令 cd + 目录 cat + 文件名,我们需要输入两次,点击两次 enter 有没有 ...
- Linux重复执行上条命令
Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行.2.按 !! 并回车执行.3.输入 !-1 并回车执行.4.按 Ctrl+P 并回车执行.
- Linux下间隔多少秒 (即以秒为单位) 去执行某条命令或某个shell脚本的操作方法【转】
在日常运维工作中, 经常会碰到以秒为单位去定时执行某些命令或监控脚本的需求. 说到定时任务就要用到crontab,通常来说,crontab的最小单位是分钟级别,要想实现秒级别的定时任务,就要进行特殊设 ...
- PowerShell一次执行多条命令
PowerShell一次执行多条命令语句 使用CMD之后换到PS之后想一次执行多条命令会很不习惯,因为原来的&&语句连接符已经不能用了. 在各种搜索后没有发现网上有说明这个的.无奈只能 ...
- supervisor的command执行两条命令
如下supervisor的进程的comand配置参数只能写一个命令 1.要执行多条命令,可以写个sh文件包含多条命令,然后sh -x xxxx.sh,但这样又多了一个文件, 2.把所有命令放在字符 ...
- linux sheel重复执行上条命令
Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行. 2.按 !! 并回车执行. 3.输入 !-1 并回车执行. 4.按 Ctrl+P 并回车执行.
- ssh 执行多条命令包含awk的用法
格式:ssh user@ip command 单条命令:ssh user@ip command1 多条命令:ssh user@ip "command1;command2" 不加双引 ...
- [教程]K8Cscan调用外部程序(Win/Linux批量上控/执行多条命令/保存结果)
0x000 调用原理 Cscan调用外部程序有两种方式,一是编写DLL,二是配置文件 编写DLL文件对于不懂编程的人来说可能会很难(虽然支持各语言) 由于考虑到很多人不会编程或会编程又急用无法短时间转 ...
- 【转】使用shell登录远程服务器执行多条命令,ssh登录之后执行脚本文件
原文:https://blog.csdn.net/qq_36622490/article/details/100773589 这个需求主要是我在jenkins中pipeline的代码里,需要使用she ...
- Linux连续执行多条命令
引自:这里 每条命令使用";"隔开,则无论前边的命令执行成功与否都会继续执行下一条命令这里,故意将第二条命令中的echo多写了一个o,命令执行出错,但并不影响后续命令的执行可以这么 ...
随机推荐
- 二:【nopcommerce系列】Nop的文件结构,引用关系。如何编译打包部署等
如果,你还没先看第一篇,先看看 一:[nopcommerce系列]Nop整体架构的简单介绍,在看nop代码之前,你需要懂哪些东西 如果你确定你已经看完了第一篇,并且真的理解 mvc.和autofac, ...
- 深入理解python的yield和generator
原文发表在我的博客主页,转载请注明出处 前言 没有用过的东西,没有深刻理解的东西很难说自己会,而且被别人一问必然破绽百出.虽然之前有接触过python协程的概念,但是只是走马观花,这两天的一次交谈中, ...
- .net 估计要死在你手里了
最近不太爽,想换工作,上这些知名的招聘网站,一搜 .net 心凉了一截,很少有大公司用.net,工资也不是很高. 不用我多说什么,想必很多人应该有类似经历,只是打了牙往肚子里咽. 来两副图: 最近用滴 ...
- .Net分布式异常报警系统-客户端及服务端API
客户端 客户端的作用就是捕获未处理异常, 发送异常到服务端. 关于捕获未处理异常的方法参考 http://www.cnblogs.com/youring2/archive/2012/04/25/246 ...
- Eclipse自动补全功能管理
#这种方法只适用于Eclipse Classic版本(这个版本带有插件的源码) 在使用Eclispe的过程,感觉自动补全做的不好,没有VS的强大.下面说两个增强自动补全的方法: 1.增加Eclipse ...
- python3.4 build in functions from 官方文档 翻译中
2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...
- MapReduce编程示例
1.将hadoop插件放入eclipse/plugins目录中 2.eclipse配置hadoop 依赖包目录 Window—Preferences 3.新建Map/Reduce Project项目 ...
- 1415-2 计科&计高 软件工程博客&Github地址汇总-修正版
序号 标识 博客 代码 1 1121袁颖 joanyy joanyy 2 1122崔琪 chitty ChittyCui 3 1123吕志浩 lucy123 715lvzhihao 4 1124张静 ...
- HIbernate的增删改
数据库是oracle 以一对多为例:user50一的一方 order50是多的一方 首先是实体类: 这里的实体是双向关系,既通过user50可以找到order50,通过order50可以找到 ...
- MyEclipse10连接数据库
连接oracle数据库 DB窗口>>右键:新建