简体   繁体   中英

Display the result of a command prompt in Windows 10

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

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.

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://docs.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM