I'm trying to get the result of command with wmic.exe on my C# windows form application. I achieved it in my code, but I wants to hide the window that opens for a half of second and then closed, when I call to my function.
This is the code:
Process p = new Process();
p.StartInfo.FileName = "wmic.exe";
p.StartInfo.Arguments = "A COMMAND";
p.StartInfo.UserName = "";
SecureString secure = new SecureString();
foreach (char c in "")
{
secure.AppendChar(c);
}
p.StartInfo.Password = secure;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return output;
Related
I'm trying to run cmd from local folder and wanted to pass multiple commands at the same time Eg: once i'm able to run this command, p.StandardInput.WriteLine("multichain-util create chain34");after executing this i want to pass this command p.StandardInput.WriteLine("multichai-cli chain 34 -daemon")
class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.WorkingDirectory = #"D:\multichain-windows-2.0.2";
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.StandardInput.WriteLine("multichain-util create chain34");
p.StandardInput.Flush();
p.StandardInput.Close();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());
Console.ReadKey();
}
}
Move all process related stuff into a method. Take in a list of commands as a parameter for that command. And then instead of one p.StandardInput.WriteLine(), iterate through the list of commands and write them all.
static void Main()
{
var commands = new List<string>() { "dir", "mkdir ABC", "dir" };
ExecuteCmd(commands);
Console.ReadLine();
}
static void ExecuteCmd(List<string> commands)
{
Process p = new Process();
p.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
foreach (var comm in commands)
p.StandardInput.WriteLine(comm);
p.StandardInput.Flush();
p.StandardInput.Close();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
I've try to taken python version, so i've start a process with cmd and a command "python --version".
I've try this for first:
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k C:/Python36/python --version";
p.StartInfo.CreateNoWindow = false;
var retorno = p.Start();
}
and opened a cmd window and returned this:
cmd return
instead of this, i need this result returned to my WPF application, so i try this:
public static string GetPythonVersion()
{
string command = "python --version";
string output = null;
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format(#"/c {0}\{1} ", "C:/Python36/", command);
p.StartInfo.CreateNoWindow = true;
if (p.Start())
output = p.StandardOutput.ReadToEnd();
}
return output;
}
return empty string to me.
however to an example if i using the same code to return "pip list" to my wpf application working well, but in this case to taken the version the string return empty....
Ok, guys.I've discovered what is wrong on the code. Below the correct code:
public static string GetPythonVersion()
{
string command = "python --version";
string output = null;
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format(#"/c {0}\{1} ", "C:/Python36/", command);
p.StartInfo.CreateNoWindow = true;
if (p.Start())
output = p.StandardError.ReadToEnd();
}
return output;
}
yeah, the python version is on StandardError, because, as you can see on :
https://docs.python.org/3/using/cmdline.html#generic-options
"Print the Python version number and exit."
So when run the command on cmd, at the StandardOutput there is nothin...
thanks to everyone for trying to help me ! now this case is over!
I'm trying to execute wmic command on C# and get the output, but the function is only returning first line and the command which is not running.
Code:
private static String wimc(String cmd)
{
var psi = new ProcessStartInfo("wmic");
psi.Arguments = #"shadowcopy call create Volume='C:\'";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
var p = Process.Start(psi);
p.WaitForExit();
String output = p.StandardOutput.ReadToEnd();
return output;
}
Output on C#:
Executing (Win32_ShadowCopy)->create()
Only show first line and command not working
Cmd output(expected)
Executing (Win32_ShadowCopy)->create() Method execution successful. Out Parameters: instance of __PARAMETERS {
ReturnValue = 0;
ShadowID = "{B2FDCFDE-7C48-4F96-9648-9A15DB89506C}";
};
shadowcopy on cmd was created with sucess
For redirecting wmic to the console output you need to add /OUTPUT:STDOUT to your arguments.
And of course you will need to run your C# application as administrator.
var psi = new ProcessStartInfo("wmic");
psi.Arguments = #"/OUTPUT:STDOUT shadowcopy call create Volume='C:\'";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var p = Process.Start(psi);
p.WaitForExit();
String output = p.StandardOutput.ReadToEnd();
String errOutput = p.StandardError.ReadToEnd();
Here is what worked for me.
System.Diagnostics.ProcessStartInfo usbDevicesInfo = new System.Diagnostics.ProcessStartInfo("wmic", "path CIM_USBDevice get Caption");
usbDevicesInfo.RedirectStandardOutput = true;
usbDevicesInfo.UseShellExecute = false;
usbDevicesInfo.CreateNoWindow = true;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = usbDevicesInfo;
process.Start();
process.WaitForExit();
Console.WriteLine("ExitCode: " + process.ExitCode.ToString() + "\n");
result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
I would like to redirect the Process's standard output to a string for later parsing.
I would also like to see the output on the screen, while the process is running, and not only when it finishes it's run.
Is that even possible?
Use RedirectStandardOutput.
Sample from MSDN:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Also see OutputDataReceived and BeginOutputReadLine() for an alternative to ReadToEnd(), that will better fulfill your "see output while the process is running" requirement.
If you want to execute an exe from your c# application and get the output from it then you can use the below code
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "PATH TO YOUR FILE";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.EnableRaisingEvents = true;
p.Start();
svgText = p.StandardOutput.ReadToEnd();
using(StreamReader s = p.StandardError)
{
string error = s.ReadToEnd();
p.WaitForExit(20000);
}
Don't forgete to write p.EnableRaisingEvents = true;
New to programming and I'm writing a process which is supposed to open up the cmd prompt, run the command
"/k nslookup 123.123.123.123;
And then redirect the standard output into a string so the data can be manipulated. I've tried various combinations but cannot get the program to output anything except my last line of "Press any key to close".
I feel as though I'm missing something very simple as I can't find anything wrong with my code. Does anyone have any suggestions?
try
{
string strCmdText;
strCmdText = "/k nslookup 123.123.123.123";
// Start the process.
Process p = new Process();
//The name of the application to start, or the name of a document
p.StartInfo.FileName = "C:/Windows/System32/cmd.exe";
// On start run the string strCmdText as a command
p.StartInfo.Arguments = strCmdText;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadLine();
p.WaitForExit();
Console.WriteLine(output);
}
catch (Exception)
{
Console.WriteLine( "error");
}
//Wait for user to press a button to close window
Console.WriteLine("Press any key...");
Console.ReadLine();
I think it is because the command prompt is not exiting. Instead of passing arguments, write them to the standard input and then exit, like this:
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("/k nslookup 123.123.123.123");
p.StandardInput.WriteLine("exit");
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);