User enter the command programmatically in the cmd - c#

I want to build a windows application that can prompt CMD. However I would like to enable user enter the command programmatically. As in my code below, I enable to called CMD but the command (e.g. ipconfig) is only hardcoded. I want user to enter other command.
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("ipconfig");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string s = process.StandardOutput.ReadToEnd();
richTextBox1.Text = s;
}

You can do this....
// you can populate command from user input
String command = "ipconfig";
// or
command = userTxt.Text;
process.StandardInput.WriteLine(command );

Related

External EXE's Output is Blank (or at least appears)

Pretty new to C#, I'm trying to create a WinForms Application that will run ScanState and capture the output from it. I've got this code as a test...
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = $"amd64\\scanstate.exe"; ;
process.StartInfo.Arguments = "";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
string q = "";
string err = "";
while (!process.HasExited)
{
q += process.StandardOutput.ReadToEnd();
err += process.StandardError.ReadToEnd();
}
//label1.text = q;
MessageBox.Show(q.ToString());
MessageBox.Show(err);
}
But all I get is two blank message boxes.
Inspecting q shows that apparently it says something.
The inspector says this:
\r\0\n\0s\0t\0r\0a\0t\0o\0r\0 \0p\0r\0i\0v\0i\0l\0e\0g\0e\0s\0 \0a\0r\0e\0 \0r\0e\0q\0u\0i\0r\0e\0d\0 \0t\0o\0 \0r\0u\0n\0 \0t\0h\0i\0s\0 \0t\0o\0o\0l\0.\0\r\0\n\0\r\r\nScanState return code: 34\r\r\n"
I'm not sure what this is or if it can even be converted to plain text.

Calling git clone from C# app doesn't return output data

I am calling git clone from a C# application by creating a new process and it calls git successfully but no ouput is returned to my delegate. This means I can't provide any updates to the user as the process progresses. The Clone_DataOutputReceived method is not being called at all.
public void CloneRepo(string repoUrl, string dataPath)
{
var process = new Process();
process.StartInfo.FileName = #"git.exe";
process.StartInfo.Arguments = $"clone {repoUrl}";
process.StartInfo.WorkingDirectory = dataPath;
process.OutputDataReceived += Clone_DataOutputReceived;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
}
private void Clone_DataOutputReceived(object sender, DataReceivedEventArgs e)
{
// This method is not being called
// Expecting d.Data to be populated with the line from the output
}
I think you want to set FileName to "cmd.exe" and then issue process.SendCommand("git.exe") to the cmd instance.

How to get the cmd command output in c# to a lable

I wrote a code for run cmd commands from c# form application. Now I want to get the output of the cmd to a lable in winform. I wrote a code. but it is giving me following error
Screenshot of the Error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: StandardOut has not been redirected or the process hasn't started yet.
how to fix it? this is my original code.
private void button1_Click(object sender, EventArgs e)
{
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 = "/C ipconfig";
startiNFO.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo = startiNFO;
process.Start();
string outp = process.StandardOutput.ReadToEnd();
process.WaitForExit();
MessageBox.Show(outp);
}
instead of process.WaitForExit(); do something like this
The complete code for your function would look something like this
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C ipconfig";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
string q = "";
while(!process.HasExited)
{
q += process.StandardOutput.ReadToEnd();
}
label1.text = q;
MessageBox.Show(q);
}

How to hide the Command Window using c#

Building a console app that will execute an exe file(pagesnap.exe). I would like to hide its window(pagesnap.exe) during execution. How is it done.
ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\psexec.exe";
Psi.Arguments = #"/C \\DESK123 D:\files\pagesnap.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Psi.WindowStyle = ProcessWindowStyle.Hidden;
Psi.CreateNoWindow = true;
Process.Start(Psi).WaitForExit();
DESK123 is the local PC. Would later try this with remote PCs.
Things I have tried
Psi.Arguments = #"/C start /b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = #"/b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = #"/C psexec \\DESK123 /b D:\files\pagesnap.exe";
Psi.Arguments = #"/C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 output.log";
Update: I have built pagesnap with Output type as a windows application instead of console. The cmd window doesn't come up, now. Seems this is the only way for me
Simply call the following function. Pass the argument as your command and your working directory
private string BatchCommand(string cmd, string mapD)
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
procStartInfo.WorkingDirectory = mapD;
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process cmdProcess = new System.Diagnostics.Process();
cmdProcess.StartInfo = procStartInfo;
cmdProcess.ErrorDataReceived += cmd_Error;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine("ping www.google.com"); //Execute ping
cmdProcess.StandardInput.WriteLine("exit"); //Execute exit.
cmdProcess.WaitForExit();
// Get the output into a string
return Batchresults;
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Batchresults += Environment.NewLine + e.Data.ToString();
}
void cmd_Error(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Batchresults += Environment.NewLine + e.Data.ToString();
}
}

pass multiple arguments to an EXE from Windows form Application

I have an app.exe application that asks to enter input path string, once i enter, it asks output path string... now when i enter, app.exe perform some operation
i need to pass these paths from my Window Form Application
i saw a lot of questions like this but could not implement what i require because i never worked with processes and Stream Reader or Writer
any help please... examples will be thanked.. thank you..
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.WaitForExit(3000);
process.Close();
ok i tried that
but its giving some exception
StandardOut has not been redirected or the process hasn't started yet...
my code was
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Arguments = input + ";" + output;
process.Start();
string Strout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
You can use ProcessStartInfo.Arguments for this.
Process process = new Process()
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
....
process.Arguments = input + " " + output;

Categories

Resources