我想执行批处理命令并将输出保存在字符串中,但我只能执行文件而无法将内容保存在字符串中。
批处理文件:
@回声关闭
“C:\lmxendutil.exe”-licstatxml -主机 serv005 -端口
6200>C:\Temp\HW_Lic_XML.xml 记事本 C:\Temp\HW_Lic_XML.xml
C#代码:
private void btnShowLicstate_Click(object sender, EventArgs e)
{
string command = "'C:\\lmxendutil.exe' -licstatxml -host lwserv005 -port 6200";
txtOutput.Text = ExecuteCommand(command);
}
static string ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
exitCode = process.ExitCode;
process.Close();
return output;
}
我想要以字符串形式输出,并直接在 C# 中执行此操作,无需使用批处理文件,这可能吗?
不需要使用“CMD.exe”来执行命令行应用程序或检索输出,您可以直接使用“lmxendutil.exe”。
尝试这个:
processInfo = new ProcessStartInfo();
processInfo.FileName = "C:\\lmxendutil.exe";
processInfo.Arguments = "-licstatxml -host serv005 -port 6200";
//etc...
进行修改以在那里使用“命令”。
我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)