从 C# 运行 bat 脚本
我正在尝试从 ac Sharp 程序中运行批处理脚本,我正在使用的代码如下所示:
Process proc = new Process();
proc.StartInfo.FileName = "G:\\Media\\Downloads\\print.bat";
proc.Start();
该脚本很简单(用于测试目的)并包含一行:
echo hello > output.txt
当我从 Windows 资源管理器运行该脚本时,它可以工作,但不能从 C# 代码运行时。
有什么想法吗?
另外,我如何为进程提供完成时的回调方法?
谢谢
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
When I run the script from windows explorer it works, but does not when run from the C# code.
any thoughts?
Also how can I give the processes a callback method for when it has finished?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它对我来说效果很好。我猜发生的情况是,当您以编程方式执行批处理文件时,您期望在
Downloads
文件夹中创建输出文件,而实际上它是在应用程序的< /em> 文件夹完全限定批处理文件中的输出文件路径或更改已启动进程的工作目录,如下所示:
至于有关进程完成时接收通知的问题,您可以使用
WaitForExit
方法或 < a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx" rel="nofollow">退出
Process 对象上的 > 事件。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 folderEither fully qualify the output file-path in the batch file or change the working directory of the launched process, like this:
As for your question about receiving notification when the process has finished, you can use the
WaitForExit
method or theExited
event on theProcess
object.