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.