简体   繁体   中英

Execute windows 10 IOT cmd command from c# code

Windows10 IOT enterprise has a feature to protect write access on drives. This feature is known as UWF "Unified Write Filter". I enable this feature and protect write access on C drive. Now I am looking for a functionality to disable it through my c# code. Cmd command to disable it is "uwfmgr filter disable". I implemented code (below) to execute this command, but its not working

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

The code is executed without giving any error but the command does not execute.

You aren't waiting for the process to finish, and are not looking for any errors.

You need to call process.WaitForExit and take a look at 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}");
}

See this page for an example https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=netframework-4.8#System_Diagnostics_Process_StandardError

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