How to stop the cmd window from closing - c#

I run PsExec in a WPF application, but after execution, the window closes.
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#"\\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
I also tried:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#" \K \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
but nothing happens here. A window appears only for a second.
How can I stop the window from closing? Why doesn't "WaitForExit" do it?

Try running:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
process.StartInfo.Arguments = String.Format(#"/k C:\Windows\SysWoW64\PsExec64.exe \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
WaitForExit did not work for you because PsExec64.exe does not wait for user input at all. It take commands as an argument >> parse & run it >> exit process. So technically your "code" did wait for PsExec64.exe to exit and then continued.

Related

C# Run console application from another app in a new console window

The second app is a console application and I want to see it's output window.
I know how to use Process.Start() but it doesn't show the console window for the app.
This is what I have tried:
Process.Start("MyApp.exe", "arg1 arg2");
So how to do it?
Perhapse this helps:
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.CreateNoWindow = false;
info.UseShellExecute = true;
Process processChild = Process.Start(info);
I figured it out. I have to run cmd command with /k argument (to keep the console window open) and then my whole command-line:
var command = "MyApp.exe arg1 arg2";
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/k " + command);
processStartInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
//In case you need the output. But you have to wait enough for the output
//string text = process.StandardOutput.ReadToEnd();

C# starts some cmd commands but other doesn't

My windows forms app triggers an event:
using System.Diagnostics;
string strCmdText = "'/C ping server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, cmd.exe is getting spawned, runs for a while, the output is not displayed, but it is present in the redirected 1.txt file.
However, I need to run query command:
using System.Diagnostics;
string strCmdText = "'/C query user /server:server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, it spawns a cmd.exe but just for 1 second, then it dissapears, and the output is not present in the 1.txt file.
Is there any way to see what the query command does before it disappears, like keep it open when executing? Maybe is something interesting in there.
Or, am I doing something wrong? Maybe I need to run the command otherwise?
This way:
string outputProcess = "";
string errorProcess = "";
using (Process process = new Process())
{
process.StartInfo.FileName = yourPath;
process.StartInfo.Arguments = yourArguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
outputProcess = process.StandardOutput.ReadToEnd();
errorProcess = process.StandardError.ReadToEnd();
process.WaitForExit();
}
if you really want to run the code as yours. just replace the "CMD" to "CMD /k"

C# child process behaviour after parent crash

I have a simple yet interesting question.
I need to start a process (a node.js server) from a C# application.
I found a piece of code explaining how to start the server from within the application.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"c:\node\node.exe";**//Path to node installed folder****
string argument = "\\ bundle\main.js";
p.StartInfo.Arguments = #argument;
p.Start();
My question is : what happend with this process if the parent process (the C# application) crashes ? Will the child process exit/crash or will it keep running as it's a totally separate program ?
In the case it keeps running, is there a way to "link" those two processes to make sure the child exits if the parent crashes ?
Tried you code like as below , if you want close your process you have to use this function process.CloseMainWindow(); this will close process you started.
public static void Main()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
process.StartInfo = startInfo;
process.Start();
Console.ReadLine();
process.CloseMainWindow();
process.Close();
}
In above code i started cmd.exe and as it has its own window i code CloseMainWindow() function to close that program.
as Cmd is separate process it will not get close when you close program which started it.
will this process keep running if the parent crashes (before the process.CloseMainWindow();) ?
answer to question is : Cmd process will not get closed if parent get closed before calling CloseMainWindow
try this for checking it
public static void Main()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
process.StartInfo = startInfo;
process.Start();
throw new Exception("test");
Console.ReadLine();
process.CloseMainWindow();
process.Close();
}
in above code main program got exception and it get created before closing child process then in that case child process will run.
if you want to close External started process forcefully when application close , than make use of Application.ApplicationExit Event, and make call to CloseMainWindow() function of external process object.

How to cancel an execution of a command window in C#?

So I'm using visual studio to run a command line to get a "dir /s >
c:\log.txt".
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 dir " + drivePath + " /s > c:\\log.txt";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I'm wanting to put this in a backgroundWorker so that I'm able to cancel it if the process is taking too long. But I don't see any way to cancel the execution of command line since the execution of the command is outside the scope of the program. Is there some logic I can add to this so that it will at least act as if I've cancelled it?

Running a command shell from C#

I want to run a command from C# with below code but the application waits infinitely:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.Start();
p.WaitForExit();
p.StandardOutput.ReadToEnd();
I did a debug and the last break point passed is p.WaitForExit(). What am I doing wrong?
By the way, I tried run the command manually and I get the result in miliseconds.
Try prepending a /C to your arguments - without it, cmd.exe won't terminate

Categories