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 != 0){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);

转载于:https://www.cnblogs.com/fdyang/p/3701527.html

(C#) 调用执行批处理文件相关推荐

  1. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取注入的 libbridge.so 动态库中的 load 函数地址 并 通过 远程调用 执行该函数 )

    文章目录 一.dlsym 函数简介 二.获取 目标进程 linker 中的 dlsym 函数地址 三.远程调用 目标进程 linker 中的 dlsym 函数 获取 注入的 libbridge.so ...

  2. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取 linker 中的 dlopen 函数地址 并 通过 远程调用 执行该函数 )

    文章目录 一.dlopen 函数简介 二.获取 目标进程 linker 中的 dlopen 函数地址 三.远程调用 目标进程 linker 中的 dlopen 函数 一.dlopen 函数简介 dlo ...

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

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

  4. 命令行,使用java的java 命令,直接调用执行class文件

    目录 0.最基本使用,参照下面链接(以前整理的资料) 1.直接调用java文件■cmd 2.调用jar包(Main Class打包到 jar包时) 3.补充说明 4.运行时,设置系统变量 5.查看类运 ...

  5. 使用java的java 命令,直接调用执行class文件

    使用java的java 命令,直接调用执行class文件_sun0322-CSDN博客_java 执行class 更多操作: 命令行,使用java的java 命令,直接调用执行class文件_sun0 ...

  6. Jenkins执行批处理文件、powershell失败

    Jenkins执行批处理文件.powershell失败 报错信息如下: [test.91huayi.com] $ powershell.exe -NonInteractive -ExecutionPo ...

  7. Java调用执行python脚本

    Java调用执行python脚本 一.python脚本执行方式 二.Java调用执行python脚本 三.关闭停止进程(端口)命令 四.java调用执行关闭进程杀掉相关进程 一.python脚本执行方 ...

  8. python执行批处理文件_如何从Python中执行批处理文件,从而改变调用进程的环境?...

    Is it possible to call init_env.bat from python in a way that allows init_env.bat to alter the envir ...

  9. nodejs在cmd中运行js_如何在nodejs里调用执行系统命令

    每种语言都有自己的优势,互相结合起来各取所长程序执行起来效率更高或者说哪种实现方式较简单就用哪个,nodejs是利用子进程来调用系统命令或者文件,文档见nodejs.org/api/child_pro ...

最新文章

  1. python两个乘号_Python小白入门:第一讲||变量和简单数据类型
  2. PL\SQL 打开时出现动态执行表不可访问,本会话的自动统计被禁止
  3. 正则仅输入文字字母空格,但必须含有字母
  4. 互联网产品用户体验设计的三大定律
  5. ionic android绝对定位,Safari中的CSS故障与Ionicons作为背景内容和绝对定位
  6. html5 开发工具_前端HTML5开发工具有哪些呢?
  7. mysql 表自动复制_mysql-10临时表、复制表
  8. Spring mvc配置文件
  9. ISP缓存机制及登录串号现象
  10. dojo——AMD(二、AMD中class内部成员函数相互调用实现)
  11. java框架注入是创建对象吗_spring之IOC容器创建对象
  12. AngularJs学习笔记--directive
  13. express学习 - (3)express 路由
  14. dos2unix离线安装
  15. 小米便签源码分析——gtask包
  16. Microbiome:鸡肠道微生物宏基因集的构建(张和平、魏泓、秦楠点评)
  17. app内接入高德地图常用设置及工具方法整理
  18. python的永久存储——pickle‘泡菜’ ,‘腌制’天气查询脚本
  19. 机房用计算机桌,机房电脑桌椅的正常尺寸是多少
  20. 计算机相关专业学生必读书单

热门文章

  1. gateway集成sentinel实现网关限流
  2. Python超越Java语言,跃居世界编程语言第2位了!你却还在犹豫学不学Python?
  3. springboot学习笔记(二)
  4. python中的 and以及or
  5. Vitalik Buterin:Casper 权益证明与分片技术最新进展
  6. php excel中文名称乱码,phpexcel导出中文文件名乱码的解决办法
  7. php的主要架构,php运行原理与基本结构
  8. python堆堆乐教程_python堆排序,详细过程图和讲解,这样做小白都会
  9. 正则式简介及常用正则式
  10. python测试网站功能_Python检测网站链接是否已存在