0

I'm making an application where I have to enable and disable the UWF in Windows 10. But I want to intercept the success or failure, the problem is that when I only displays a letter.

string output = string.Empty;
        string error = string.Empty;


        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c uwfmgr.exe volume protect c:");
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.RedirectStandardError = true;

        processStartInfo.UseShellExecute = false;
        processStartInfo.Verb = "runas";
        Process process = Process.Start(processStartInfo);

        using (StreamReader streamReader = process.StandardOutput)
        {
            output = streamReader.ReadToEnd();
        }

        using (StreamReader streamReader = process.StandardError)
        {
            error = streamReader.ReadToEnd();
        }

        if (!string.IsNullOrEmpty(error))
        {
            MessageBox.Show("Error: " + error);
            return;
        }
        MessageBox.Show("OK: " + output);

Here comes the message box "OK U"

Thanks

fede186
  • 89
  • 8

2 Answers2

0

Thank you for your reply. I tried to read the articles but given my lack of experience I can apply what it says, Could you give me a little extra help? The thing I noticed is that I can enable the UWF only if you use the following code

ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/k uwfmgr.exe volume protect " + cmbBox_Disk.SelectedItem.ToString().Substring(0, 2) + " & exit");
processStartInfo.CreateNoWindow = false;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;
processStartInfo.Verb = "runas";
Process process = Process.Start(processStartInfo);


processStartInfo = new ProcessStartInfo("cmd", "/k uwfmgr.exe filter enable & exit");
processStartInfo.CreateNoWindow = false;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;
processStartInfo.Verb = "runas";
process = Process.Start(processStartInfo);

but the problem is that in this way if there is an error I can not agree with the user.

fede186
  • 89
  • 8
0

You can read the output of the shell, see this answer:

Process.start: how to get the output?

Unfortunately there is no good way to detect errors using process. It would be better to use WMI and UWF's WMI provider:

See: https://learn.microsoft.com/en-us/windows-hardware/customize/enterprise/uwf-wmi-provider-reference

Make sure you read the docs thoroughly though. For example, in order to protect a volume you need to run Protect(); on an instance which has 'currentSession = false'. This instance needs to be created by yourself. It has some small caveats here an there.

sommmen
  • 6,570
  • 2
  • 30
  • 51