Stop command line before closing so fast

I am trying to troubleshoot why the following function is not working.

public void RunCmd() { string strCmdText; strCmdText = "/C [enter command stuff here]"; System.Diagnostics.Process.Start("CMD.exe", strCmdText); } 

However, whenever I try to start it or throw some breakpoints, a command opens, shows an error, and then closes very quickly (so fast that I can’t read anything).

Is there a way to stop the program or find out what is happening? Breakpoints don't seem to work.

When I directly type it on the command line instead of starting with this C # script, the command works just fine.

+4
source share
7 answers

try the following:

  strCmdText = "/K [enter command stuff here]"; 

/ C Executes the command indicated by the line and then exits

/ K Executes the command indicated by the string, but remains

+14
source

Maybe try adding a pause command?

+2
source

There are various options. Using /K prevent the window from closing.

You can also edit your command to add SLEEP after the main call. For example, the following command will wait 2 seconds before exiting:

 public void RunCmd() { string strCmdText = "/C \"[enter command stuff here]\" & \"SLEEP 2\""; System.Diagnostics.Process.Start("CMD.exe", strCmdText); } 
+1
source

Process.Start has two parameters: the name of the process and the arguments to go to the process. CMD has a problem that understands that the argument "[enter" is. If this is not an argument that CMD understands, it will exit immediately.

0
source

cin.get ()

This waits for a keystroke from the user.

0
source

create a temporary folder under c: then add "<c: \ temp \ log.txt" at the end of the command. this writes the output to a file

0
source

In addition to the hint about using / K vs / C to create a shell that is β€œdelayed”, you probably want to enter an empty β€œset” command to see which environmental variables (and paths) are set in the spawned shell. In all likelihood, the difference between the successful launch of your script in your own shell session and the failure in the generated shell are the parameters of the environment in which it is running.

0
source

Source: https://habr.com/ru/post/1495011/


All Articles