Apoorva Asthana
Apoorva Asthana

Reputation: 76

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.

Upvotes: 0

Views: 399

Answers (1)

Neil
Neil

Reputation: 11899

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://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=netframework-4.8#System_Diagnostics_Process_StandardError

Upvotes: 1

Related Questions