Mohammad H
Mohammad H

Reputation: 805

Prevent command prompt from closing after running code (visual C#)?

I'm trying to run a code on command prompt using this code:

ProcessStartInfo startInfo = new ProcessStartInfo("Cmd");
startInfo.Arguments = "/c tracert 8.8.8.8";
Process.Start(startInfo);

But it closes after running code but I want cmd stay open. What should I do?

Upvotes: 0

Views: 2184

Answers (3)

Uzair Ahmed Siddiqui
Uzair Ahmed Siddiqui

Reputation: 364

Try

startInfo.Arguments = "/K /c tracert 8.8.8.8";

Upvotes: -1

Eugene Sh.
Eugene Sh.

Reputation: 18299

Add a pause command

ProcessStartInfo startInfo = new ProcessStartInfo("Cmd");
startInfo.Arguments = "/c tracert 8.8.8.8 & pause";
Process.Start(startInfo);

Upvotes: 1

JohnKiller
JohnKiller

Reputation: 2008

The /c argoument is telling CMD to close after the instructions are done.

Instead, if you want to keep the window open and return to the terminal, you should use the /k switch:

ProcessStartInfo startInfo = new ProcessStartInfo("Cmd");
startInfo.Arguments = "/k tracert 8.8.8.8";
Process.Start(startInfo);

Upvotes: 6

Related Questions