How to execute cmd in C # and then execute the following command in the same window?

That's right what they try to execute, this is a program that basically sets the active section to 1 click, saving time and the ability to use the cmd hint, etc.

I looked at the System.Management namespace but couldn't figure out how to use it :(

So, I resorted to using CMD, I have a module application written in C #, and basically I want to run "DISKPART", which then starts diskpart in the cmd window, then I want to ask it to "Select drive 0", and then "select partition 1" and then "active".

Doing this in CMD on your own works fine, but it turned out to be inconvenient with the application :( What I managed to do was run DiskPart in one window using Process.Start, and then open it to open a new window and run the following code snippet, but since the new the window did not start diskpart cmd, it does not work> :(

Any suggestions?

Thanks!

Ash

+4
source share
4 answers

Until you make decisions on the output, you can create a batch file in your C # application and run it through Process.Start(...) .

You need to create two files.

First runDiskPart.bat :

  diskpart / s myScript.dp

Second myScript.dp :

  ... some commands ...
 exit

Obviously, the names are completely arbitrary, but the /s directive should refer to the name of your second file.

+3
source

After some searching, I think you can do what you want with the script file. Read this .

Thus, you can run diskpart /s script.txt using Process.Start after creating the script.txt file with the necessary commands.

+3
source

What about introducing a delay like Thread.Sleep (1000), so another process has time to complete the first command?

0
source

What you really want to do is wait for the program to exit, and then move on to the next call. Take a look at the question .

0
source

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


All Articles