简体   繁体   English

启动批处理文件

[英]Launching a batch file

I have the following code: 我有以下代码:

String Antcbatchpath = @"C:\GUI\antc.bat";

System.Diagnostics.Process runantc = new System.Diagnostics.Process();
runantc.StartInfo.FileName = Antcbatchpath;
runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();

Will this load the batch file from C:\\GUI\\antc.bat ? 这将从C:\\GUI\\antc.bat加载批处理文件吗?

Or runantc.StartInfo.FileName is only for a root directory? 还是runantc.StartInfo.FileName仅用于根目录? Root directory is where the application is located 根目录是应用程序所在的位置

EDIT 1: 编辑1:

hi instead of @"C:\\GUI\\antc.bat" i have a path: 您好,而不是@“ C:\\ GUI \\ antc.bat”,我有一条路径:

String Antcbatchpath =@"C:\GUI Lab Tools\Build Machine\antc.bat";

which essentially contains white spaces. 基本上包含空格。 will it affect the runantc.StartInfo.Filename = Antcbatchpath; 是否会影响runantc.StartInfo.Filename = Antcbatchpath; ?

UseShellExecute = true should do it. UseShellExecute = true应该可以做到。

Alternatively, if you need redirection, use: 或者,如果需要重定向,请使用:

runantc.StartInfo.FileName = "CMD.EXE";
runantc.StartInfo.Arguments = "/C " + Antcbatchpath;

You can try to set WorkingDirectory to prevent any ambiguity, but in my experience, it is not necessary. 您可以尝试设置WorkingDirectory以防止任何歧义,但是根据我的经验,这不是必需的。

The problem you're having is because antc.bat is not an executable. 您遇到的问题是因为antc.bat不是可执行文件。 It requires UseShellExecute to be true, but that would prevent you from redirecting the output. 它要求UseShellExecute为true,但这会阻止您重定向输出。 I guess you will have to choose either one. 我想您将不得不选择其中之一。

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

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