views:

382

answers:

4

Hey Guys,

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

A: 

What about introducing a delay, such as Thread.Sleep(1000), so that the other process has time to complete the first command?

JYelton
+2  A: 

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.

Austin Salonen
A Batch file sounds really interesting, i have managed to get it to start "diskpart" but same issue as before how do i hard code what it should do next? On my screen it show "DISKPART>" waiting for input, how do i now add a command to get this to work?Thank for speedy response guys btw! :)
Ash
@Ashh: I've updated my answer.
Austin Salonen
FWIW, I found all this by running `diskpart /?` from the command prompt.
Austin Salonen
AHH!! I see that is awesome! Thank you! I looked in the help but ive not used alot of DOS and never used batch files before. This is awesome! Thank you so much!Ash
Ash
A: 

What you really want to do is wait for the program to exit and then move onto the next invocation. Take a look at this question.

rerun
+1  A: 

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

You can therefore run diskpart /s script.txt with Process.Start after creating a script.txt file with your necessary commands.

IVlad