繁体   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