【问题标题】:Display the result of a command prompt in Windows 10在 Windows 10 中显示命令提示符的结果
【发布时间】:2017-03-30 10:20:40
【问题描述】:

我正在制作一个必须在 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”来了

谢谢

【问题讨论】:

  • 在 cmd.exe 中运行相同的命令会得到什么输出?

标签: c# windows windows-10


【解决方案1】:

感谢您的回复。 我试图阅读这些文章,但由于我缺乏经验,我可以应用它所说的内容,你能给我一些额外的帮助吗? 我注意到的是,只有使用以下代码才能启用 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);

但问题是这样如果有错误我不能同意用户。

【讨论】:

  • 你应该删除这个答案 - 你不想在答案中问更多问题:-)
  • 我尝试在评论中添加它,但告诉我字符太多
  • 我在我的答案中添加了代码 - 并对其进行了多次编辑......看看它现在是否符合您的要求。 :-)
【解决方案2】:

你可以阅读shell的输出,看这个答案:

Process.start: how to get the output?

不幸的是,没有很好的方法来使用 process 来检测错误。 最好使用 WMI 和 UWF 的 WMI 提供程序:

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

请确保您彻底阅读了文档。例如,为了保护卷,您需要运行 Protect();在具有“currentSession = false”的实例上。这个实例需要自己创建。 它在这里和那里有一些小警告。

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2022-06-20
    • 2019-02-27
    相关资源
    最近更新 更多