(C#) 调用执行批处理文件
Task: 在Windows的Service里面定时的调用执行一个批处理文件。
private ApplicationOutput RunCommandOnPC(string executablePath, string args, string workingFolder, bool ignoreErrorCode)
{
if (!Path.IsPathRooted(executablePath))
{
string executingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
executablePath = Path.Combine(executingDirectory, executablePath);
}
// 显示Dos窗口,观察执行情况。
//using (Process process = new Process())
//{
// process.StartInfo = new ProcessStartInfo(executablePath, args);
// process.StartInfo.UseShellExecute = true; // process.Start();
// process.WaitForExit();
//}
//return null; using (Process process = new Process())
{
process.StartInfo = new ProcessStartInfo(executablePath, args);
if (workingFolder != null)
{
process.StartInfo.WorkingDirectory = workingFolder;
} process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder stdOutput = new StringBuilder();
StringBuilder stdError = new StringBuilder(); using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
stdOutput.AppendLine(e.Data);
}
else
{
outputWaitHandle.Set();
}
}; process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
stdError.AppendLine(e.Data);
}
else
{
errorWaitHandle.Set();
}
}; string processOutput = string.Empty;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine(); if (process.WaitForExit((int)this.defaultTimeout.TotalMilliseconds)
&& outputWaitHandle.WaitOne((int)this.defaultTimeout.TotalMilliseconds)
&& errorWaitHandle.WaitOne((int)this.defaultTimeout.TotalMilliseconds))
{
// Process is completed.
processOutput = stdOutput.ToString() + stdError.ToString();
if (!ignoreErrorCode && process.ExitCode != )
{
throw new Exception(string.Format("{0} {1}, ExitCode {2}, Args {3}.", executablePath, args, process.ExitCode, processOutput));
}
}
else
{
throw new Exception(string.Format("Process running is time out in {0}.", (int)this.defaultTimeout.TotalMilliseconds));
} return new ApplicationOutput
{
ReturnValue = (uint)process.ExitCode,
Output = processOutput
}; }
}
调用程序的时候,需要用 cmd.exe /c
string dosCommand = @"c:\windows\system32\cmd.exe";
string batchFileName = @"test.bat";
string workingFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string args = string.Format(@"/C {0}\{1} /Y", workingFolder, batchFileName); var output = this.RunCommandOnPC(dosCommand, args, workingFolder, false);
(C#) 调用执行批处理文件的更多相关文章
- WinRAR 自动解压 解压完成后,执行批处理文件
		部分内容参考网页:http://bbs.kafan.cn/thread-1243208-1-1.html WinRAR 的自动解压文件功能使压缩包也能像 Setup 程序那样,双击后显示一个软件许可, ... 
- PHP通过反射方法调用执行类中的私有方法
		PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 下面我们演示一下如何通过反射,来调用执行一个类中的私有方法: <?php //MyClass这个类中包 ... 
- 如何使用T-SQL备份还原数据库及c#如何调用执行?  C#中索引器的作用和实现。  jquery控制元素的隐藏和显示的几种方法。  localStorage、sessionStorage用法总结  在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一)  span<T>之高性能字符串操作实测
		如何使用T-SQL备份还原数据库及c#如何调用执行? 准备材料:Microsoft SQL Server一部.需要还原的bak文件一只 一.备份 数据库备份语句:user master backup ... 
- 在window下, Java调用执行bat脚本
		参考博客: https://www.cnblogs.com/jing1617/p/6430141.html 最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下: 我这里是先 ... 
- rpyc + plumbum 实现远程调用执行shell脚本
		rpyc可以很方便实现远程方法调用, 而plumbum则可以实现在python中类似shell的方式编码: 具体实现代码如下: Server.py import rpyc from rpyc.util ... 
- java 中 this 和 super 说明及在构造器中super()和this()相互调用执行顺序
		this this 表示当前对象 使用上细分的话,this有 this. 和this()的使用情况 ,下面我们开始细撸 this . 使用场景一: 在成员方法中,this.变量名 指带当前对象的变量, ... 
- 《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
		1.简介 在做web自动化时,有些情况selenium的api无法完成,需要通过第三方手段比如js来完成实现,比如去改变某些元素对象的属性或者进行一些特殊的操作,本文将来讲解怎样来调用JavaScri ... 
- 《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)
		1.简介 在实际工作中,我们需要对处理的元素进行高亮显示,或者有时候为了看清楚做跟踪鼠标点击了哪些元素需要标记出来.今天宏哥就在这里把这种测试场景讲解和分享一下. 2.用法 创建一个执行 JS 的对象 ... 
- Linux下编译生成SO并进行调用执行
		Linux下编译生成SO并进行调用执行 参考博客的博客: C编译: 动态连接库 (.so文件) - Vamei - 博客园 (cnblogs.com) C 多个动态库存在同名函数问题处理方法:-fvi ... 
随机推荐
- (转)Hadoop数据类型
			来源: http://www.cnblogs.com/anny-1980/articles/2608097.html BooleanWritable:标准布尔型数值 ByteWritable:单字节数 ... 
- timus 1022 Genealogical Tree(拓扑排序)
			Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ... 
- (转) TensorFlow深度学习,一篇文章就够了
			TensorFlow深度学习,一篇文章就够了 2016/09/22 · IT技术 · TensorFlow, 深度学习 分享到:6 原文出处: 我爱计算机 (@tobe迪豪 ) 作者: 陈迪 ... 
- What are the main disadvantages of Java Server Faces 2.0?
			http://stackoverflow.com/questions/3623911/what-are-the-main-disadvantages-of-java-server-faces-2-0/ ... 
- 谷歌大牛Jeff Dean是如何成为互联网战神的
			“光在真空中的速度曾经是大约每小时35英里,然后Jeff Dean花了一个周末优化了基础物理学.”——出自“关于Jeff Dean的事实” 其实,“关于Jeff Dean的事实”这个G+帖中描述的并非 ... 
- linearlist和linkedlist的区别 待整理
			线性表在内存中是一块连续的存储空间:如:一个表中的内容是:[1,2,3]则它在内存中可能是如下存储的:1 2 3 优点:查找 通过这个结构可以看出,只要知道了第一个元素在内存中所在的位置. ... 
- WCF Data Service 使用小结 —— 了解OData(一)
			最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不多,但它确实是个很好的 ... 
- sed命令拷屏
			http://blog.sina.com.cn/s/blog_45497dfa0100w6r3.html sed样例较多,可以参考 http://blog.sina.com.cn/s/blog_6d ... 
- nginx-gridfs使用
			安装nginx及nginx-gridfs 依赖库.工具 # yum -y install pcre-devel openssl-devel zlib-devel # yum -y install ... 
- 【转】JSONP跨域的原理解析
			JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制,被称为“Same-Origin Policy”(同源策略).这一策略对于Java ... 
