Reading Output from cmd getting errors - c#

i have been looking up "how to get standard output from cmd" and i found a few tutorials, but none, yes NONE seemed to work, im trying to read all the output that "cmd.exe" has for me.
heres the whole code, Scroll down for the error location
public static string C(string arguments, bool b)
{
System.Diagnostics.Process process = new
System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
string res = "";
if (b)
{
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd());
}
string r = q.ToString();
res = r;
}
if(res == "" || res == null)
{
return "NO-RESULT";
}
return res;
}
Where i get my error (System.InvalidOperationException: 'StandardOut has not been redirected or the process hasn't started yet.')
string res = "";
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd()); // Right here
}
string r = q.ToString();
res = r;

You are creating a ProcessStartInfo named startInfo, then set some properties on process.StartInfo and then assign startInfo to process.StartInfo basically reverting what you set previously.
You should be setting RedirectStandardOutput, RedirectStandardInput and UseShellExecute on the startInfo:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();

Related

Create a process that execute powershell in C# but failed outpt

I dont know where is my missing. Even it dit not write out log. Could you fix my test code:
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = #"/C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex) {
Debug.Print("Error: " + ex.Message);
}
}
Finally got it to work, code should look like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/C powershell.exe /C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
This worked for me! You can use full paths to cmd.exe and powershell.exe if you feel necessary, it's not that important here.

DoxBox start from C#

I try to start DoxBox from my console application, but it simply do nothing. What can be a problem?
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "cmd.exe";
string strComm = "DoxBox.exe /dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();
Why do you start the procces with cmd.exe?
It isn't in your C:\\Program Files (x86)\\DoxBox directory, I'm sure.
Provide the DoxBox file in the FileName, without cmd.exe at all:
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "DoxBox.exe";
string strComm = "/dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();

C# Process Standard Input

I am currently trying to disconnect from a network folder through the command line and am using the following code:
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C NET USE F: /delete";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process2.StartInfo = startInfo;
process2.Start();
StreamWriter sw = process2.StandardInput;
sw.WriteLine("Y");
sw.Close();
process2.WaitForExit();
process2.Close();
Occasionally, I get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which I want to reply "Y", but I seem to be having issues with that working.
Does anyone know why my code is not inputting "Y" to standard input?
Use below code to get the message "Is it ok to continue disconnecting and force them closed? (Y/N) [N]", to which reply "Y"
static void Main(string[] args)
{
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C NET USE F: /delete";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
process2.StartInfo = startInfo;
process2.Start();
Read(process2.StandardOutput);
Read(process2.StandardError);
while (true)
process2.StandardInput.WriteLine("Y");
}
private static void Read(StreamReader reader)
{
new Thread(() =>
{
while (true)
{
int current;
while ((current = reader.Read()) >= 0)
Console.Write((char)current);
}
}).Start();
}
I think this may help you..

Command line run error

After looking on forums, I have written this snippet:
public string ExecuteCmd()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = this.m_command;
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
The m_command is member of a class, initialized in the constructor.
For my tests, it is net user. When the compiler arrives at this point, I get the following exception:
StandardOut has not been redirected or the process hasn't started yet.
Where is my mistake?
You need this:
//....
startInfo.Arguments = "/C " + this.m_command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//....

C# Multiple Input and Output to a Single CMD Process

Hi how do i enter a command to a cmd process which is already started by my application and also to redirect the output back. After that i want to enter my input again.
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.FileName = "cmd.exe";
psi.Arguments = "/k";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
Process p = new Process();
p.StartInfo = psi;
p.Start();
while(true)
{
p.StandardInput.WriteLine(Console.ReadLine());
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
Console.Read();
}

Categories