简体   繁体   English

如何用C#编写bat脚本

[英]How to write bat script in C#

I have .bat script in VisualStudio and I call them in C# code.我在 VisualStudio 中有 .bat 脚本,我在 C# 代码中调用它们。 Here is a script and my C# code where I call script.这是一个脚本和我调用脚本的 C# 代码。 How I can write directly .bat script in my C# code in Process?如何在 Process 中的 C# 代码中直接编写 .bat 脚本?

//bat script
set SERVICENAME=%1%
arbstate -t %SERVICENAME%

//C# code
Process process = new Process();
process.StartInfo.FileName = "Scripts\\CheckServiceState.bat";
process.StartInfo.Arguments = serviceName;

BAT file are interpreted by the command shell cmd.exe. BAT 文件由命令外壳 cmd.exe 解释。 So you have to start cmd.exe and pass the path to the BAT file as parameter.所以你必须启动 cmd.exe 并将路径作为参数传递给 BAT 文件。 See here for an example and background information: Why does Process.Start("cmd.exe", process);有关示例和背景信息,请参见此处: 为什么 Process.Start("cmd.exe", process); not work? 不行?

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

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