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#) 调用执行批处理文件的更多相关文章

  1. WinRAR 自动解压 解压完成后,执行批处理文件

    部分内容参考网页:http://bbs.kafan.cn/thread-1243208-1-1.html WinRAR 的自动解压文件功能使压缩包也能像 Setup 程序那样,双击后显示一个软件许可, ...

  2. PHP通过反射方法调用执行类中的私有方法

    PHP 5 具有完整的反射 API,添加了对类.接口.函数.方法和扩展进行反向工程的能力. 下面我们演示一下如何通过反射,来调用执行一个类中的私有方法: <?php //MyClass这个类中包 ...

  3. 如何使用T-SQL备份还原数据库及c#如何调用执行? C#中索引器的作用和实现。 jquery控制元素的隐藏和显示的几种方法。 localStorage、sessionStorage用法总结 在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一) span<T>之高性能字符串操作实测

    如何使用T-SQL备份还原数据库及c#如何调用执行? 准备材料:Microsoft SQL Server一部.需要还原的bak文件一只 一.备份 数据库备份语句:user master backup ...

  4. 在window下, Java调用执行bat脚本

    参考博客: https://www.cnblogs.com/jing1617/p/6430141.html 最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下: 我这里是先 ...

  5. rpyc + plumbum 实现远程调用执行shell脚本

    rpyc可以很方便实现远程方法调用, 而plumbum则可以实现在python中类似shell的方式编码: 具体实现代码如下: Server.py import rpyc from rpyc.util ...

  6. java 中 this 和 super 说明及在构造器中super()和this()相互调用执行顺序

    this this 表示当前对象 使用上细分的话,this有 this. 和this()的使用情况 ,下面我们开始细撸 this . 使用场景一: 在成员方法中,this.变量名 指带当前对象的变量, ...

  7. 《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)

    1.简介 在做web自动化时,有些情况selenium的api无法完成,需要通过第三方手段比如js来完成实现,比如去改变某些元素对象的属性或者进行一些特殊的操作,本文将来讲解怎样来调用JavaScri ...

  8. 《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)

    1.简介 在实际工作中,我们需要对处理的元素进行高亮显示,或者有时候为了看清楚做跟踪鼠标点击了哪些元素需要标记出来.今天宏哥就在这里把这种测试场景讲解和分享一下. 2.用法 创建一个执行 JS 的对象 ...

  9. Linux下编译生成SO并进行调用执行

    Linux下编译生成SO并进行调用执行 参考博客的博客: C编译: 动态连接库 (.so文件) - Vamei - 博客园 (cnblogs.com) C 多个动态库存在同名函数问题处理方法:-fvi ...

随机推荐

  1. 【转】SQL Server与Oracle的区别

    转自:http://soft.chinabyte.com/database/255/12258255.shtml SQL Server与Oracle的区别 2012-02-10 00:00 中国IT实 ...

  2. JS初学之-js的a:hover

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  3. dedecms 忘记后台密码

    找到admin表 dede_admin,把其pwd的值修改为 默认的 字符串:f297a57a5a743894a0e4, 之后,你的密码就被充值为 admin

  4. UVA-11468 Substring(AC自动机+DP)

    题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...

  5. 【HAOI2009】【P1307】毛毛虫

    感觉相比其他树归题简单多了,不过有点绕(也许是我的思路很奇怪一.一)(这是省选题啊,就算作为T1这题也太水了,HA好弱……) 原题: 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一 ...

  6. Netty系列之Netty可靠性分析

      作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单   1. 背景 1.1. 宕机的代价 1.1. ...

  7. 利用C#Marshal类实现托管和非托管的相互转换

    Marshal 类 命名空间:System.Runtime.InteropServices 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与 ...

  8. C++ vector和list的区别

    1.vector数据结构vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存 ...

  9. 非root模式下安装mysql php小记

    假设你的home目录为/home/work mysql-server 安装 1. 下载mysql.tar.gz wget http://dev.mysql.com/get/Downloads/MySQ ...

  10. php 数组排序 sort asort ksort

    <?php $arr = array('d'=>'sdf', 'r'=>'sdf', 'a'=> 'eee'); //sort($arr); // 对数组的值进行重排, 删除之 ...