I need to run an elevated process from C#, and I need it to redirect input and output. Since doing all three is impossible, I think I may have come up with a workaround, but it doesn't seem to work.
Process p = new Process();
string command1 = "/s " + path + "\\script1.script";
p.StartInfo = new ProcessStartInfo("diskpart",command1)
{
UseShellExecute = true,
CreateNoWindow = false,
Verb="runas"
};
p.Start();
p.WaitForExit();
That works. It calls DiskPart with a script that just does list volume
.
I want to capture the output from that into outfile1.txt
.
When I run the following command from the terminal it works:
diskpart /s script1.script>outfile1.txt
However, when I modify command1
to be "/s " + path + "\\script1.script>+path+"\\outfile1.txt"
I get an error from DiskPart that it was unable to open or read my file.
What am I doing wrong?
EDIT
Didn't find a solution, but I found a workaround.
I was trying to get the drive letter of a USB drive which I then wanted to set as readonly. However, using the DriveInfo.Name
I was able to get it. (It would have been more helpful if that were called DriveInfo.driveLetter
) Then I wrote my DiskPart script accordingly.