0

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.

AvailableName
  • 666
  • 4
  • 13
  • 24

2 Answers2

0

You might have a syntax error in your command1. You said you changed it to:

"/s " + path + "\script1.script>+path+"\outfile1.txt"

and I think what you want is(missing one double quotation mark):

"/s " + path + "\script1.script>" + path + "\outfile1.txt"

If that's not the case, what happens when you use your terminal command - "diskpart /s script1.script>outfile1.txt" - as command1? You might want to look at this other stackoverflow topic for running commands in C#:

Run Command Prompt Commands

Community
  • 1
  • 1
Shaz
  • 1,376
  • 8
  • 18
0

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.

AvailableName
  • 666
  • 4
  • 13
  • 24