我怎么能执行在C#中直接一个批处理命令? [英] How I can execute a Batch Command in C# directly?

查看:653
本文介绍了我怎么能执行在C#中直接一个批处理命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有C#和批处理文件的问题。我想不执行批处理命令和输出保存到C#中的字符串。但我只能执行该文件,但不是在一个字符串保存此内容,并显示在这一个文本框。

hi i have a question to c# and batch files. I want do execute a batch command and save the output in a string in c#. but i can only execute the file but not save this content in a string and show this in a textbox.

我的批处理文件:

关闭@echo

C:\\ lmxendutil.exe-licstatxml -host serv005 -port
  6200> C:\\ TEMP \\ HW_Lic_XML.xml记事本C:\\ TEMP \\ HW_Lic_XML.xml

"C:\lmxendutil.exe" -licstatxml -host serv005 -port 6200>C:\Temp\HW_Lic_XML.xml notepad C:\Temp\HW_Lic_XML.xml

下面是我的C#code:

Here is my c# code:

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; 
}

我要输出I A stirng,如果我能做到这一点,我想,我没有一个批处理文件。我想这样做batvchcommand直接在C#...我能做到这一点?

I want the output i a stirng and if i can do this i want that i not have a batch file. i want do this batvchcommand direct in c# ...can i do this?

推荐答案

不需要用的CMD.exe用于执行命令行应用程序或retreive输出,可以用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#中直接一个批处理命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆