(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 ...
随机推荐
- javabean实现serializable有什么用?为什么数据库持久就Bean实现这个接口?
Java的"对象序列化"能让你将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象时候,你就能把这些byte数据恢复出来,并据此重新构建那个对象了 ...
- c 深度剖析 6
函数的编码风格 1.注释 2,空行 3,缩进 4,参数长度,代码长度,语句长度要合适. 5,少用全局变量 6,指针仅作输入参数时,可用const 设置其为只读属性,避免其在函数中被修改. 7,函数默认 ...
- 转:db2 iptables相关用法(1)
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- c#部分---结构体再利用;
//定义一个结构体,存放关于车辆的几个信息 //将所有车的信息都放入集合中 //车型号 价格(W) 轴距 (mm) 油耗(L/100km) //宝马320Li 38 2920 6.9 //宝马520L ...
- 《Java程序设计》第二次实验报告
20145220 <Java程序设计>第二次实验报告 课程:Java程序设计 指导教师:娄嘉鹏 实验日期:2016.04.12 实验名称:Java面向对象程序设计 实验内容 初步掌握单元测 ...
- img元素底部有空白间距的问题
<div style="width:100px;height:100px"><img src="./1.jpg"></div> ...
- Java——File(文件)
public static void main(String[] args) { // getFile(); /* * 需求: 对指定目录进行所有内容的列出,(包含子目录中的内容) * */ ...
- 【转】非常适用的Sourceinsight插件,提高效率事半功倍
原文网址:http://www.cnblogs.com/wangqiguo/p/3713211.html 一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用 ...
- MapReduce: 一个巨大的倒退
前言 databasecolumn 的数据库大牛们(其中包括PostgreSQL的最初伯克利领导:Michael Stonebraker)最近写了一篇评论当前如日中天的MapReduce 技术的文章, ...
- SSRS入门相关笔记
1.SSRS Server 的地址的查看及设置:打开 开始->程序-> Microsoft SQL Server 2012/2014 -> Configuration Tools - ...