简体   繁体   English

从C#运行bat脚本

[英]Running bat script from C#

I am trying to run a batch script from within ac sharp program the code I am using is shown below: 我正在尝试从ac sharp程序中运行批处理脚本,我正在使用的代码如下所示:

 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

When I run the script from windows explorer it works, but does not when run from the C# code. 当我从Windows资源管理器运行脚本时它可以工作,但是从C#代码运行时却不行。

any thoughts? 有什么想法吗?

Also how can I give the processes a callback method for when it has finished? 另外,如何为进程提供一个回调方法?

Thanks 谢谢

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 我猜测正在发生的是当你以编程方式执行批处理文件时,你期望在Downloads文件夹中创建输出文件,当它实际上是在应用程序的文件夹中创建的时候

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\";

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. 至于关于在进程完成时接收通知的问题,可以在Process对象上使用WaitForExit方法或Exited事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM