0

I have a Windows Forms Application with the app.manifest set to:

level="requireAdministrator" uiAccess="false".

Now I need to run a console application (Diskspd.exe) from my application and return the standard output. It almost works, though the part that requires runAs Admin of DiskSpd fails. Here is the part of the code that call the process (Example of txtDatPath.Text is "C:\temp\IO.dat"):

    if (File.Exists(txtDatPath.Text))
    {
        File.Delete(txtDatPath.Text);
    }
    string qPath = string.Format("\"{0}\"", txtDatPath.Text);
    if (rdoOLTP.Checked)
    {
        DScmd = "-b8K –d180 -h -L –o32 –t3 -r –w75 -c5G " + qPath;
    }
    else
    {
        DScmd = "–b60K –d60 -h -L –o32 –t1 -s –w100 –c1G " + qPath;
    }
    //now set up the cmd
    var p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.Verb = "runas";
    p.StartInfo.Arguments = DScmd;
    p.StartInfo.FileName = "diskspd.exe";
    p.Start();

    // To avoid deadlocks, always read the output stream first and then wait.  
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

I need the process itself to run as Admin to succeed. The command behaves the same way if I run regular command then try CMD and run as admin it completes.

StartInfo.UseShellExecute needs to be set to false for the RedirectStandardOutput to work.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Gary P.
  • 7
  • 4
  • If your winforms app is `requireAdministrator`, any process you start should also be running as administrator already. – Dark Falcon Jul 14 '22 at 23:19
  • When running inside VS you are not automatically Run As Admin. You must right click VS shortcut and select Run As Admin. The exe produced by VS will run automatically As Admin when run from windows. – jdweng Jul 15 '22 at 00:57
  • https://stackoverflow.com/questions/18660014/redirect-standard-output-and-prompt-for-uac-with-processstartinfo – Jeremy Lakeman Jul 15 '22 at 02:24
  • VS and app.manifest are all set to run as admin. Though still fails. – Gary P. Jul 15 '22 at 16:50
  • The following may be helpful: https://stackoverflow.com/a/71344930/10024425 – Tu deschizi eu inchid Jul 15 '22 at 17:20
  • Reviewed the link and implemented the complete code as described - no joy! Tried different variations of the sample provided - same results. It's like the file creation of the qPath (using example: D:\IO.dat) is not allowed by the system using Microsoft's DISKSPD.EXE app. – Gary P. Jul 18 '22 at 20:18

1 Answers1

0

Well - after all of this - in adding debugging, the issue is the p.StartInfo.Arguments = DScmd;

The Argument is being interpreted as separate entries.

The solution/problem: DISKSPD requires ASCII and the string is UNICODE. Changed the hyphens to convert.ToChar(45)

So I am going to close this case (thx all!) and open new one.

Gary P.
  • 7
  • 4