ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "tsvc -a -st rifs -h "+textBox1+" -sn "+textBox2+" -status";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
I need to run a command in cmd that will take 2 parameters that will be inserted to textBox1 and textBox2 and then sending the output to the richTextBox1.
When running it this way I get :
first chance exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: StandardOut has not been redirected or the process hasn't started yet
I tried to exclude the Output line , and when i do it does run CMD but does not type in any command (It just opens a CMD window and does nothing ) .
Process.Start() asynchronously starts a new process. When you get to the process.StandardOutput.ReadToEnd() the process is not finished yet, thus the exception. You should use eventing to hook into the OutputDataRecieved event. You want to do something like this:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1 + " -sn " + textBox2 + " -status";
process.StartInfo = startInfo;
process.OutputDataReceived += ProcessOnOutputDataReceived;
process.ErrorDataReceived += ProcessOnOutputDataReceived;
process.Start();
and then add an event handler to the Output data recieved like so:
private void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
richTextBox1.Text += dataReceivedEventArgs.Data;
}
Also, I am not certain but i think you need to call text on your textboxes:
startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1.Text + " -sn " + textBox2.Text + " -status";
Managed to do it in the End with .
richTextBox1.Text = "";
int counter = 0 ,totalMemory=0;
string line;
string command = " /c ro -a -h " + textBox1.Text + " -sn HC* $$info Server Total = $servermemory Service Memory = $servicememory > c:\\noc\\ntb\\logs\\output.txt";
ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
Process proc = new Process();
procStartInfo.CreateNoWindow = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
Related
I am trying to display the output of a command-window in a textbox, but the code doesn't update. It only shows the first action, nothing more. What I want is it to update if new text appears in the commandline aswell.
Here is the code I have so far, which all get triggered on a button-click:
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string result = proc.StandardOutput.ReadToEnd();
TextAreaOutput.AppendText(result + "\n");
}
What am I missing?
how to pass file path and parameters to shell.shellExecute command.
For example I am trying following
:shell.ShellExecute("L:\\test\\test.exe",["/abc /pqr /xyz"]);
Here, abc,pqr,xyz are the parameters required to open the file test.exe
You can try using the below command where you can send the required arguments using
process.StartInfo.Arguments
Below is the Sample code .
using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
Instead of directly using shell you can use process to call your exe with parameter. Below is the sample code
private static void RunExeWithParameter(string exePath, string parameter1, string parameter2)
{
string error = "";
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "\"" + exePath + "\"";
process.StartInfo.Arguments = "\"" + parameter1 + " \"" + parameter2 + "\"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
error = process.StandardError.ReadToEnd();
process.Close();
}
if (error.Trim().Trim("\r\n".ToCharArray()).Trim() != "")
{
throw new Exception(error);
}
}
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();
}
}
I want to redirect standardoutput of a Process in a richTextBox. Here is my process configuration,
string command = "/K perl C:\\Server.pl ";
ProcessStartInfo startInfo = new ProcessStartInfo();
Process proc = new Process();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
proc.StartInfo = startInfo;
proc.OutputDataReceived += (s, ea) => this.richTextBox1.AppendText(ea.Data);
proc.Start();
proc.BeginOutputReadLine();
Here is my Server.pl file
print "Server1 \n";
while(1)
{
print "Server \n";
sleep 1;
}
But when I run the program the cmd.exe is just black and nothing printed in richTextBox. but when I change the
startInfo.RedirectStandardOutput = false;
I have this out put in my cmd.exe:
Server1
Server
Server
Server
...
How I can work around this issue ?
Might be as simple as disabling output buffering in your perl script. This is done using the $| special variable (see perlvar).
$| = 1;
print "Server1 \n";
...
I have created a Process to run command in CMD.
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
How can I run this command without displaying actual CMD window?
You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Source:
Property:MSDN
Enumartion: MSDN
And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c apktool d app.apk";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "apktool";
//join the arguments with a space, this allows you to set "app.apk" to a variable
psi.Arguments = String.Join(" ", "d", "app.apk");
//leave it to the application, not the OS to launch the file
psi.UseShellExecute = false;
//choose to not create a window
psi.CreateNoWindow = true;
//set the window's style to 'hidden'
psi.WindowStyle = ProcessWindowStyle.Hidden;
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
The main issues:
using cmd /c when not necessary
starting the app without setting the properties for hiding it
Try this :
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;