問題描述
我想執行一個批處理命令并將輸出保存在一個字符串中,但是我只能執行該文件并且無法將內容保存在一個字符串中.
I want to execute a batch command and save the output in a string, but I can only execute the file and am not able to save the content in a string.
批處理文件:
@echo 關閉
"C:lmxendutil.exe" -licstatxml -host serv005 -port6200>C:TempHW_Lic_XML.xml 記事本 C:TempHW_Lic_XML.xml
"C:lmxendutil.exe" -licstatxml -host serv005 -port 6200>C:TempHW_Lic_XML.xml notepad C:TempHW_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# 中執行此操作而無需批處理文件,這可能嗎?
I want the output in a string and do this directly in C# without a batch file, is this possible?
推薦答案
不需要使用CMD.exe"來執行命令行應用程序或檢索輸出,您可以直接使用lmxendutil.exe".
Don't need to use "CMD.exe" for execute a commandline application or retreive the output, you can use "lmxendutil.exe" directly.
試試這個:
processInfo = new ProcessStartInfo();
processInfo.FileName = "C:\lmxendutil.exe";
processInfo.Arguments = "-licstatxml -host serv005 -port 6200";
//etc...
進行修改以在此處使用命令".
Do your modifications to use "command" there.
我希望這會有所幫助.
這篇關于如何直接在 C# 中執行批處理命令?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!