Thursday, December 30, 2010

Change Registry Entry in C# (DWORD value)

Editing the remote registry is easy with C#'s WMI/Registry provider. I've recently implemented these features into my MobileExec Program to disable CAC access. See below:



ConnectionOptions co = new ConnectionOptions();
                        co.Username = txtUsername.Text;
                        co.Password = txtPassword.Text;
                        co.Impersonation = ImpersonationLevel.Impersonate;


                        co.EnablePrivileges = true;
                        co.Authentication = AuthenticationLevel.PacketPrivacy;


                        System.Net.IPHostEntry h =
                    System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                        string IPAddress = h.AddressList.GetValue(0).ToString();
                        string lm = System.Net.Dns.GetHostName().ToString();




                        if (txtUsername.Text == "" || txtPassword.Text == ""
                     || txtServer.Text == "127.0.0.1"
                     || txtServer.Text.ToLower() == "localhost"
                     || txtServer.Text.ToLower() == lm.ToLower()
                     || txtServer.Text.ToLower() == IPAddress.ToLower())
                        {
                            co.Username = null;
                            co.Password = null;
                        }
                        string temp1 = txtServer.Text;




                        Label1.ForeColor = Color.Black;


                        this.Label1.Style["text-align"] = "left";


                        try
                        {
                            ManagementScope myScope =
                                new ManagementScope("\\\\" + temp1 + "\\root\\default", co);
                            ManagementPath mypath = new ManagementPath("StdRegProv");
                            ManagementClass wmiRegistry =
                                new ManagementClass(myScope, mypath, null);


                            const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
                            
                            string keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
                            ManagementBaseObject inParams6 = wmiRegistry.GetMethodParameters("SetDWORDValue");
                            


                            inParams6["hDefKey"] = HKEY_LOCAL_MACHINE;
                            inParams6["sSubKeyName"] = keyPath;
                            inParams6["sValueName"] = "SCForceOption";
                            inParams6["uValue"] = (UInt32)0;


                            ManagementBaseObject outParams6 = wmiRegistry.InvokeMethod("SetDWORDValue", inParams6, null);
                            Label1.Text = "Done! CAC Disabled";
                        }
                        catch (Exception ex)
                        {
                            Label1.Text = ex.Message;
                        }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...