簡體   English   中英

在 Windows 10 中顯示命令提示符的結果

[英]Display the result of a command prompt in Windows 10

我正在制作一個應用程序,我必須在 Windows 10 中啟用和禁用 UWF。但我想攔截成功或失敗,問題是當我只顯示一個字母時。

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);

消息框“OK U”來了

謝謝

感謝你的回復。 我試着閱讀這些文章,但鑒於我缺乏經驗,我可以應用它所說的,你能給我一些額外的幫助嗎? 我注意到的是,只有當您使用以下代碼時,我才能啟用 UWF

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);

但問題是這種方式如果出現錯誤我不能同意用戶的意見。

您可以閱讀 shell 的輸出,請參閱此答案:

Process.start:如何獲得輸出?

不幸的是,沒有使用過程檢測錯誤的好方法。 最好使用 WMI 和 UWF 的 WMI provider:

請參閱: https : //docs.microsoft.com/en-us/windows-hardware/customize/enterprise/uwf-wmi-provider-reference

不過,請確保您徹底閱讀了文檔。 例如,為了保護卷,您需要運行 Protect(); 在具有“currentSession = false”的實例上。 這個實例需要自己創建。 它在這里和那里有一些小警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM