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. zoj3261 带权并查集

    题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现在发生战争,有一些联系通道会被摧毁,而一些星球会通过还没有被摧毁的联系通道直接或者间接联系能够联系到的武力值最高的星球求救,如果有多个武力值都 ...

  2. kuangbin_UnionFind B (POJ 1611)

    过程是模板 merge完后扫一下几个跟0同祖先节点就是答案了 #include <iostream> #include <string> #include <cstdio ...

  3. (总结)Linux下使用rsync最快速删除海量文件的方法

    昨天遇到了要在Linux下删除海量文件的情况,需要删除数十万个文件.这个是之前的程序写的日志,增长很快,而且没什么用.这个时候,我们常用的删除命令rm -fr * 就不好用了,因为要等待的时间太长.所 ...

  4. springVS javaee

    https://stackoverflow.com/questions/4490682/difference-between-java-ee-and-spring-framework https:// ...

  5. distinct和group by

    distinct:同时查询两个字段,另外一个字段(不是distinct的那个字段,下例是tagid)需要多个值,如下                 group by:同时查询两个字段,另外一个字段( ...

  6. viewPage

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. SQL注入脚本(基于时间)

    #encoding=utf-8 import httplib import time import string import sys import urllib header = {'Accept' ...

  8. PHP使用session_set_save_handler陷阱

    陷阱如下 当脚本使用了session_set_save_handler 来重定向 session后,使用session_destroy后再使用session_start()重新开启session会报错 ...

  9. media type与media query

    media type(媒体类型)是css 2中的一个非常有用的属性,通过media type我们可以对不同的设备指定特定的样式,从而实现更丰富的界面.media query(媒体查询)是对media ...

  10. 017. ADO.NET Connection和command及DataReader

    ADO.NET主要包括Connection , command , DataReader, DataSet, DataAdapter5个对象, 通过这5个对象可以对数据库进行查询, 添加, 修改及删除 ...