【问题标题】:Execute windows 10 IOT cmd command from c# code从 c# 代码执行 windows 10 IOT cmd 命令
【发布时间】:2019-11-08 03:32:47
【问题描述】:

Windows10 IOT 企业版具有保护驱动器写入访问的功能。此功能称为 UWF“统一写入过滤器”。我启用此功能并保护 C 驱动器上的写访问。现在我正在寻找一种通过我的 c# 代码禁用它的功能。禁用它的 Cmd 命令是“uwfmgr 过滤器禁用”。我实现了代码(如下)来执行这个命令,但它不起作用

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new   System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C uwfmgr filter disable";
startInfo.UserName = "Administrator";
startInfo.Password  =  class1.ConvertToSecureString("SRPedm");
process.StartInfo = startInfo;
process.Start();
Process.Start("shutdown","/r /t 0");

代码执行没有给出任何错误,但命令没有执行。

【问题讨论】:

  • 我知道我的回答对你来说可能已经过时了。我用 C# 编写了一个小程序,它可能满足您的要求。 github.com/cregx/uwf-dashboard

标签: c# cmd windows-10-iot-enterprise


【解决方案1】:

您不是在等待该过程完成,也不是在寻找任何错误。

您需要致电process.WaitForExit并查看process.StandardError

public static void Main()
{
   var p = new Process();  
   p.StartInfo.UseShellExecute = false;  
   p.StartInfo.RedirectStandardError = true;  
   p.StartInfo.FileName = "Write500Lines.exe";  
   p.Start();  

   // To avoid deadlocks, always read the output stream first and then wait.  
   string output = p.StandardError.ReadToEnd();  
   p.WaitForExit();

   Console.WriteLine($"\nError stream: {output}");
}

请参阅此页面以获取示例 https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=netframework-4.8#System_Diagnostics_Process_StandardError

【讨论】:

  • 非常感谢朋友。现在我得到一个异常'System.InvalidOperationException'
  • 哪一行?异常消息是什么?
猜你喜欢
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
相关资源
最近更新 更多