How can I execute a command command in C # directly?

I want to execute a batch command and save the output to a string, but I can only execute the file and cannot save the contents to a string.

Batch file:

@ echo off

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

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 want to output to a string and do it directly in C # without a batch file, is this possible?

+10
source share
2 answers

No need to use "CMD.exe" to run a command line application or return a result, you can directly use "lmxendutil.exe".

Try the following:

 processInfo = new ProcessStartInfo(); processInfo.FileName = "C:\\lmxendutil.exe"; processInfo.Arguments = "-licstatxml -host serv005 -port 6200"; //etc... 

Make your modifications to use the "command" there.

Hope this helps.

+8
source

It doesn't seem like your batch file was giving any output. If you run it on the command line, do you see the output? You have a redirection operator > in the line of your bat string, so it seems like you are sending the output to an xml file.

If you saved the output in an XML file, perhaps you just need to load it using C # after the process is complete.

+2
source

Source: https://habr.com/ru/post/945485/


All Articles