从C#运行脚本蝙蝠 [英] Running bat script from C#

查看:163
本文介绍了从C#运行脚本蝙蝠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C锋利的程序中运行的批处理脚本中的code我使用如下:

I am trying to run a batch script from within a c sharp program the code I am using is shown below:

 Process proc = new Process();
 proc.StartInfo.FileName = "G:\\Media\\Downloads\\print.bat";
 proc.Start();

该脚本(用于测试目的)简单,包含一个行:

The script is simple (for testing purposes) and contains one line:

echo hello > output.txt

当我运行从Windows Script Explorer中它的工作原理,但不从C#code运行时。

When I run the script from windows explorer it works, but does not when run from the C# code.

什么想法?

另外我怎么可以给进程回调方法时,它已经完成了?

Also how can I give the processes a callback method for when it has finished?

感谢

推荐答案

它工作正常的我。我猜发生了什么事是,当您以编程方式执行批处理文件,你希望输出文件中的下载文件夹中创建,它实际上正在创建时在应用程序的的文件夹

It works fine for me. I'm guessing what's happening is that when you execute the batch file programmatically, you're expecting the output file to be created in the Downloads folder, when it's actually being created in the application's folder

要么完全限定在批处理文件的输出文件的路径或改变启动进程的工作目录中,如:

Either fully qualify the output file-path in the batch file or change the working directory of the launched process, like this:

proc.StartInfo.WorkingDirectory = @"G:\Media\Downloads\";

至于你提到当过程结束接到通知的问题,你可以使用的 WaitForExit 方法或的 已退出 事件流程对象。

As for your question about receiving notification when the process has finished, you can use the WaitForExit method or the Exited event on the Process object.

这篇关于从C#运行脚本蝙蝠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆