Search code examples
c#processcmd

How do I execute a cmd in C#, then in the same window execute another command that follows?


Right what im trying to accomplish is a program that basically sets the active partition in 1 click, saving the effort time and skill of using cmd prompt etc.

I have looked into the System.Management name space but couldn't work out how to use it :(

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

Doing this in CMD yourself works fine but with an application its proved to be awkward :( What ive managed to get it to do is run DiskPart fine in one window with Process.Start, then get it to open a new window and run the next piece of code but because the new window hasnt ran the diskpart cmd it doesnt work >:(

Any suggestions?

Thanks!

Ash


Solution

  • As long as you aren't making decisions on the output, you could build a batch file in your C# app and start that via Process.Start(...).

    You'll need to generate 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 needs to reference the name of your second file.