public DeleteSubKeyTree ( string subkey ) : void | ||
subkey | string | |
Результат | void |
/// <summary> /// Helper function to recursively delete a sub-key (swallows errors in the /// case of the sub-key not existing /// </summary> /// <param name="root">Root to delete key from</param> /// <param name="subKey">Name of key to delete</param> public static void DeleteSubKeyTree(RegistryKey root, string subKey) { // delete the specified sub-key if if exists (swallow the error if the // sub-key does not exist) try { root.DeleteSubKeyTree(subKey); } catch (ArgumentException) { } }
public void TestInitialize() { _rk = Registry.CurrentUser.OpenSubKey("Software", true); if (_rk.OpenSubKey(_testKeyName) != null) _rk.DeleteSubKeyTree(_testKeyName); if (_rk.OpenSubKey(_testVolatileKeyName) != null) _rk.DeleteSubKeyTree(_testVolatileKeyName); if (_rk.OpenSubKey(_testNonVolatileKeyName) != null) _rk.DeleteSubKeyTree(_testNonVolatileKeyName); if (_rk.OpenSubKey(_testRegularKey) != null) _rk.DeleteSubKeyTree(_testRegularKey); }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _madeUpKey = _madeUpKey + counter.ToString(); _rk = Registry.CurrentUser.OpenSubKey("Software", true); if (_rk.OpenSubKey(_madeUpKey) != null) _rk.DeleteSubKeyTree(_madeUpKey); if (_rk.OpenSubKey(_subKeyExists) != null) _rk.DeleteSubKeyTree(_subKeyExists); if (_rk.OpenSubKey(_subKeyExists2) != null) _rk.DeleteSubKeyTree(_subKeyExists2); }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKey += counter.ToString(); _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKey) != null) _rk1.DeleteSubKeyTree(_testKey); if (_rk1.GetValue(_testKey) != null) _rk1.DeleteValue(_testKey); //created the test key. if that failed it will cause many of the test scenarios below fail. try { _rk1 = Microsoft.Win32.Registry.CurrentUser; _rk2 = _rk1.CreateSubKey(_testKey); if (_rk2 == null) { Assert.False(true, "ERROR: Could not create test subkey, this will fail a couple of scenarios below."); } else _keyString = _rk2.ToString(); } catch (Exception e) { Assert.False(true, "ERROR: unexpected exception, " + e.ToString()); } }
/// <summary> /// Renames a subkey of the passed in registry key since /// the Framework totally forgot to include such a handy feature. /// </summary> /// <param name="regKey">The RegistryKey that contains the subkey /// you want to rename (must be writeable)</param> /// <param name="subKeyName">The name of the subkey that you want to rename /// </param> /// <param name="newSubKeyName">The new name of the RegistryKey</param> /// <returns>True if succeeds</returns> public static bool RenameSubKey(RegistryKey parentKey, string subKeyName, string newSubKeyName) { CopyKey(parentKey, subKeyName, newSubKeyName); parentKey.DeleteSubKeyTree(subKeyName); return true; }
private static void RemoveDemandLoadingEntries(bool currentUser) { try { // Choose a Registry hive based on the function input RegistryKey hive = (currentUser ? Registry.CurrentUser : Registry.LocalMachine); // Open the main AutoCAD (vertical) and "Applications" keys #if acad2012 RegistryKey ack = hive.OpenSubKey(HostApplicationServices.Current.RegistryProductRootKey); #endif #if acad2013 RegistryKey ack = hive.OpenSubKey(HostApplicationServices.Current.UserRegistryProductRootKey, true); #endif #if bcad RegistryKey ack = hive.OpenSubKey(HostApplicationServices.Current.RegistryProductRootKey); #endif using (ack) { RegistryKey appk = ack.OpenSubKey("Applications", true); using (appk) { // Delete the key with the same name as this assembly appk.DeleteSubKeyTree(Assembly.GetExecutingAssembly().GetName().Name); } } } catch { } }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKeyName += counter.ToString(); _testKeyName2 += counter.ToString(); rk1 = Microsoft.Win32.Registry.CurrentUser; if (rk1.OpenSubKey(_testKeyName2) != null) rk1.DeleteSubKeyTree(_testKeyName2); if (rk1.GetValue(_testKeyName2) != null) rk1.DeleteValue(_testKeyName2, false); if (rk1.GetValue(_testKeyName) != null) rk1.DeleteValue(_testKeyName); if (rk1.OpenSubKey(_testKeyName) != null) rk1.DeleteSubKeyTree(_testKeyName); }
public void Test04() { // [] Give subkey a value and then delete tree _rk1 = Microsoft.Win32.Registry.CurrentUser; _rk1.CreateSubKey(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) == null) { Assert.False(true, "Error Could not get subkey"); } _rk1.DeleteSubKeyTree(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) != null) { Assert.False(true, "Error SubKey still there"); } // CreateSubKey should just open a SubKeyIfIt already exists _rk2 = _rk1.CreateSubKey(_testKeyName); _rk2.CreateSubKey("BLAH"); if (_rk1.OpenSubKey(_testKeyName).OpenSubKey("BLAH") == null) { Assert.False(true, "Error Expected get not returned"); } _rk2.DeleteSubKey("BLAH"); if (_rk2.OpenSubKey("BLAH") != null) { Assert.False(true, "Error SubKey was not deleted"); } }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKeyName = _testKeyName + counter.ToString(); _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKeyName) != null) _rk1.DeleteSubKeyTree(_testKeyName); }
public void Test01() { // [] Passing in null should throw ArgumentNullException _rk1 = Microsoft.Win32.Registry.CurrentUser; Action a = () => { _rk1.DeleteSubKeyTree(null); }; Assert.Throws<ArgumentNullException>(() => { a(); }); }
private static void DeleteSetting(string path, string section) { Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path, true); if (regKey != null) { regKey.DeleteSubKeyTree(section); } }
private static bool DeleteKey(RegistryKey parent, string path) { if (KeyExists(parent, path)) { parent.DeleteSubKeyTree(path); return true; } return false; }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKey += counter.ToString(); _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKey) != null) _rk1.DeleteSubKeyTree(_testKey); if (_rk1.GetValue(_testKey) != null) _rk1.DeleteValue(_testKey); _rk2 = _rk1.CreateSubKey(_testKey); _keyString = _rk2.ToString(); }
public void UnregisterMyApp() { // Get the AutoCAD Applications key string sProdKey = HostApplicationServices.Current.UserRegistryProductRootKey; string sAppName = "AcadPalettes"; Microsoft.Win32.RegistryKey regAcadProdKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(sProdKey); Microsoft.Win32.RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true); // Delete the key for the application regAcadAppKey.DeleteSubKeyTree(sAppName); regAcadAppKey.Close(); }
/// <summary> /// Renames a subkey of the passed in registry key since /// the Framework totally forgot to include such a handy feature. /// </summary> /// <param name="regKey">The RegistryKey that contains the subkey /// you want to rename (must be writeable)</param> /// <param name="subKeyName">The name of the subkey that you want to rename /// </param> /// <param name="newSubKeyName">The new name of the RegistryKey</param> /// <returns>True if succeeds</returns> public static bool RenameSubKey(RegistryKey parentKey, string subKeyName, string newSubKeyName) { try { CopyKey(parentKey, subKeyName, newSubKeyName); parentKey.DeleteSubKeyTree(subKeyName); return true; } catch (Exception) { return false; } }
/// <summary> /// Sets the value of <paramref name="keyName"/> in <paramref name="registryKey"/> to 1 if /// <paramref name="val"/> is equal to true, otherwise sets it to 0. /// </summary> /// <param name="val"></param> /// <param name="registryKey"></param> /// <param name="keyName"></param> public static void SetRegistryKeyFromBool(bool? val, RegistryKey registryKey, String keyName) { RegistryKey newKey = registryKey.OpenSubKey(keyName, true); if (val == true && newKey == null) { registryKey.CreateSubKey(keyName); } else { if (newKey != null) { registryKey.DeleteSubKeyTree(keyName); } } if (newKey != null) { newKey.Close(); } }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKeyName = "REG_TEST_12" + counter.ToString(); _testValueName = "TestValue"; _testStringName = "TestString" + counter.ToString(); _testString = "Hello World!†þ"; _testValue = 11; _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKeyName) != null) _rk1.DeleteSubKeyTree(_testKeyName); _rk2 = _rk1.CreateSubKey(_testKeyName); _rk2.Dispose(); }
static private void DeleteUninstaller() { var UninstallRegKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; using (Microsoft.Win32.RegistryKey parent = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( UninstallRegKeyPath, true)) { if (parent == null) { throw new Exception("Uninstall registry key not found."); } string guidText = UninstallGuid.ToString("B"); parent.DeleteSubKeyTree(guidText, false); } }
public void Test03() { // [] Creating new SubKey and deleting it _rk1 = Microsoft.Win32.Registry.CurrentUser; _rk1.CreateSubKey(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) == null) { Assert.False(true, "Error SubKey does not exist."); } _rk1.DeleteSubKeyTree(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) != null) { Assert.False(true, "Error SubKey not removed properly"); } }
public static bool DeleteSubKeyTree(RegistryKey baseRegistryKey, string subKeyName) { try { RegistryKey subKey = baseRegistryKey.OpenSubKey(subKeyName); // If the RegistryKey exists, I delete it if (subKey != null) { baseRegistryKey.DeleteSubKeyTree(subKeyName); } return true; } catch { return false; } }
public void Test03() { // Give subkey a value and get it back _rk1 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(_testKeyName); _iCount = _rk1.SubKeyCount; _rk1.CreateSubKey(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) == null) { Assert.False(true, "Error Could not get subkey"); } if (_rk1.SubKeyCount != _iCount + 1) { Assert.False(true, "Error Incorrect subkeyCount"); } _rk1.DeleteSubKeyTree(_testKeyName); if (_rk1.OpenSubKey(_testKeyName) != null) { Assert.False(true, "Error SubKey still there"); } // CreateSubKey should just open a SubKeyIfIt already exists _rk1.CreateSubKey(_testKeyName); _rk2 = _rk1.CreateSubKey(_testKeyName); _rk2.CreateSubKey("BLAH"); if (_rk1.OpenSubKey(_testKeyName).OpenSubKey("BLAH") == null) { Assert.False(true, "Error Expected get not returned"); } if (_rk2.SubKeyCount != 1) { Assert.False(true, "Error Incorrect SubKeycount"); } _rk2.DeleteSubKey("BLAH"); if (_rk2.OpenSubKey("BLAH") != null) { Assert.False(true, "Error SubKey was not deleted"); } }
public void TestInitialize() { var counter = Interlocked.Increment(ref s_keyCount); _testKeyName2 += counter.ToString(); _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKeyName2) != null) _rk1.DeleteSubKeyTree(_testKeyName2); if (_rk1.GetValue(_testKeyName2) != null) _rk1.DeleteValue(_testKeyName2); _rk2 = _rk1.CreateSubKey(_testKeyName2); Random rand = new Random(10); Byte[] byteArr = new Byte[rand.Next(0, 100)]; rand.NextBytes(byteArr); _objArr = new Object[6]; _objArr[0] = (Int32)(rand.Next(Int32.MinValue, Int32.MaxValue)); _objArr[1] = (Int64)(rand.NextDouble() * Int64.MaxValue); _objArr[2] = (String)"Hello World"; _objArr[3] = (String)"Hello %path5% World"; _objArr[4] = new String[] { "Hello World", "Hello %path% World" }; _objArr[5] = (Byte[])(byteArr); }
public void Dispose() { // Clean up _rk = Registry.CurrentUser.OpenSubKey("SOFTWARE", true); if (_rk.OpenSubKey(_madeUpKey) != null) _rk.DeleteSubKeyTree(_madeUpKey); if (_rk.OpenSubKey(_subKeyExists) != null) _rk.DeleteSubKeyTree(_subKeyExists); if (_rk.OpenSubKey(_subKeyExists2) != null) _rk.DeleteSubKeyTree(_subKeyExists2); }
static void UnRegisterFiletype(string extension, RegistryKey root) { try { root.DeleteSubKeyTree("SD." + extension + "file"); } catch {} try { RegistryKey extKey; extKey = root.OpenSubKey("." + extension, true); // if no association return if (extKey == null) return; // if other association return too if ((string)extKey.GetValue("", "") != ("SD." + extension + "file")) return; // restore previous association string prev = (string)extKey.GetValue("PreSD", ""); if(prev != "") { extKey.SetValue("", prev); } extKey.Close(); if (prev != null) { root.DeleteSubKeyTree("." + extension); } } catch {} }
int LookForTheApp(string appName, RegistryKey baseKey, RegistryKey rk, string registryKeyPath, string nameField) { int appsRemoved = 0; string R_p; string keyPath; if (rk != null) { foreach (string subKeyName in rk.GetSubKeyNames()) { using (RegistryKey tempKey = rk.OpenSubKey(subKeyName)) { try { if (tempKey != null && tempKey.GetValue(nameField) != null) { Debug.WriteLine(tempKey.GetValue(nameField)); R_p = tempKey.GetValue(nameField).ToString(); if (R_p == appName) { keyPath = registryKeyPath + subKeyName; baseKey.DeleteSubKeyTree(keyPath); baseKey.Close(); foreach (DataGridViewRow dtRow in dgvInstalledApps.Rows) { if (dtRow.Cells[dtInstalledApps_Name].Value.ToString().Equals(appName)) { dgvInstalledApps.Rows.Remove(dtRow); break; } } appsRemoved++; break; } } } catch { } } } } return appsRemoved; }
private void SaveUserAppsTreeView(RegistryKey rkUser) { QTUtility.UserAppsDic.Clear(); QTUtility.dicUserAppShortcutKeys.Clear(); int separatorIndex = 1; bool flag; using(RegistryKey key = rkUser.OpenSubKey("UserApps", false)) { flag = key != null; } try { if(flag) { rkUser.DeleteSubKeyTree("UserApps"); } } catch { } using(RegistryKey key2 = rkUser.CreateSubKey("UserApps")) { foreach(TreeNode node in tnRoot_UserApps.Nodes) { if(node.Tag != null) { SaveUserAppsRegistry(node, key2, ref separatorIndex, true); } else if(node.Nodes.Count > 0) { QTUtility.UserAppsDic[node.Text] = null; SaveUserAppsRegistry_Sub(node, key2); } } } }
private void UnregisterTypeLibrary(RegistryKey rootKey) { try { ITypeLib typeLib; Guid libId; HRESULT hr = ComAPI.LoadTypeLib(TypeLibPath, out typeLib); if (hr != ComAPI.S_OK) { return; } IntPtr libAttrPtr; typeLib.GetLibAttr(out libAttrPtr); TYPELIBATTR typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(libAttrPtr, typeof(TYPELIBATTR)); libId = typeLibAttr.guid; rootKey.DeleteSubKeyTree(@"TypeLib\" + libId.ToString("B").ToUpperInvariant()); typeLib.ReleaseTLibAttr(libAttrPtr); } catch (Exception e) { Debug.Print("TypeLibHelper.UnregisterServer error : " + e); } }
public void Dispose() { _rk1 = Microsoft.Win32.Registry.CurrentUser; if (_rk1.OpenSubKey(_testKeyName) != null) _rk1.DeleteSubKeyTree(_testKeyName); if (_rk1.GetValue(_testKeyName) != null) _rk1.DeleteValue(_testKeyName); }
internal static RegistryKey RenameSubKey(RegistryKey parentKey, string subKeyName, string newSubKeyName) { RegistryKey newKey = CopyKey(parentKey, subKeyName, newSubKeyName); parentKey.DeleteSubKeyTree(subKeyName); return newKey; }
public static int Win32DeleteSubKeyTree(RegistryKey parentKey, string key) { int iRet = 0; try { parentKey.DeleteSubKeyTree(key); } catch (Exception ex) { iRet = -1; Logger.LogException("Win32DeleteSubKeyValue : ", ex); } return iRet; }
/// <summary> /// Enables or disables services for a Bluetooth device. /// </summary> /// <param name="service">The service GUID on the remote device.</param> /// <param name="state">Service state - TRUE to enable the service, FALSE to disable it.</param> /// <param name="throwOnError">Whether the method should raise an exception /// when /// </param> /// <remarks> /// When called on Windows CE, the device will require a soft-reset to enabled the settings. ///<note> /// <para>The system maintains a mapping of service guids to supported drivers for /// Bluetooth-enabled devices. Enabling a service installs the corresponding /// device driver. Disabling a service removes the corresponding device driver. /// If a non-supported service is enabled, a driver will not be installed. /// </para> /// </note> /// </remarks> /// - /// <exception cref="T:System.ComponentModel.Win32Exception">The call failed. /// </exception> public void SetServiceState(Guid service, bool state, bool throwOnError) { #if NETCF if (service == BluetoothService.SerialPort) { if (state) { //write registry settings for WM5 Serial Port support //get available ports Microsoft.Win32.RegistryKey rkPorts = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports", true); string[] supportedPorts = (string[])rkPorts.GetValue("SupportedPorts"); System.Collections.Generic.List<string> availablePorts = new System.Collections.Generic.List<string>(supportedPorts); //remove any port names from the list which are already in use string[] comPorts = System.IO.Ports.SerialPort.GetPortNames(); foreach (string comPort in comPorts) { if (availablePorts.Contains(comPort)) { availablePorts.Remove(comPort); } } //COM0 is not supported by a lot of third-party software so move it to the end of the list if present /*if (alPorts[0].ToString() == "COM0") { alPorts.RemoveAt(0); alPorts.Add("COM0"); }*/ //check availability foreach (string deviceid in rkPorts.GetSubKeyNames()) { //already configured for this device if (deviceid == this.DeviceAddress.ToString("8")) { return; } Microsoft.Win32.RegistryKey rkDevice = rkPorts.OpenSubKey(deviceid); //remove port from arraylist if unavailable string port = rkDevice.GetValue("Port").ToString(); int nullPos = port.IndexOf('\0'); if (nullPos > -1) { port = port.Substring(0, nullPos); } if (availablePorts.Contains(port)) { availablePorts.Remove(port); } rkDevice.Close(); } if (availablePorts.Count == 0) { throw new InvalidOperationException("No ports available"); } //write port details to registry Microsoft.Win32.RegistryKey rkNewPort = rkPorts.CreateSubKey(this.DeviceAddress.ToString("8")); rkNewPort.SetValue("KeepDCB", 0); rkNewPort.SetValue("RemoteDCB", 0); rkNewPort.SetValue("Encryption", 0); rkNewPort.SetValue("Authentication", 0); rkNewPort.SetValue("Port", availablePorts[availablePorts.Count-1]); rkNewPort.SetValue("Server", 0); rkNewPort.Close(); rkPorts.Close(); //try open port now try { InTheHand.Net.Ports.BluetoothSerialPort.CreateClient(availablePorts[availablePorts.Count - 1], new BluetoothEndPoint(this.DeviceAddress, BluetoothService.SerialPort)); } catch (Exception ex) { Utils.MiscUtils.Trace_WriteLine("SetServiceState failure on BluetoothSerialPort.CreateClient: " + ex); } } else { //find and remove registry entries Microsoft.Win32.RegistryKey rkPorts = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Bluetooth\\Serial\\Ports", true); foreach (string deviceAddress in rkPorts.GetSubKeyNames()) { if (deviceAddress == this.DeviceAddress.ToString("8")) { rkPorts.DeleteSubKeyTree(deviceAddress); break; } } rkPorts.Close(); } } else if (service == BluetoothService.AudioSink) { //set the enabled flag (will be reflected in UI) Microsoft.Win32.RegistryKey rkDeviceServices = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Bluetooth\\Device\\" + DeviceAddress.ToString("N") + "\\Services\\0000110b-0000-1000-8000-00805f9b34fb", true); if (rkDeviceServices != null) { rkDeviceServices.SetValue("Enabled", state ? 1 : 0); rkDeviceServices.Close(); } //set/remove as one of the 4 audio devices Microsoft.Win32.RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Bluetooth\\A2DP\\Devices"); if (rk != null) { string[] keyNames = rk.GetSubKeyNames(); foreach (string keyName in keyNames) { RegistryKey rk2 = rk.OpenSubKey(keyName, true); byte[] addressBytes = (byte[])rk2.GetValue("Address"); BluetoothAddress ba = new BluetoothAddress(addressBytes); if (ba == this.DeviceAddress) { //already in there if (!state) { //need to delete - reset to all zeros; rk2.SetValue("Address", BluetoothAddress.None.ToByteArray()); rk2.Close(); break; } } if (ba == BluetoothAddress.None) { if (state) { //need to add rk2.SetValue("Address", DeviceAddress.ToByteArray()); rk2.Close(); break; } } rk2.Close(); } rk.Close(); } } else if ((service == BluetoothService.Handsfree) | (service == BluetoothService.Headset)) { //set the enabled flag (will be reflected in UI) Microsoft.Win32.RegistryKey rkDeviceServices = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Bluetooth\\Device\\" + DeviceAddress.ToString("N") + "\\Services\\" + service.ToString("D"), true); if (rkDeviceServices != null) { rkDeviceServices.SetValue("Enabled", state ? 1 : 0); rkDeviceServices.Close(); } //set/remove the audio gateway device Microsoft.Win32.RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Bluetooth\\AudioGateway\\Devices", true); if (rk != null) { bool written = false; string[] keyNames = rk.GetSubKeyNames(); foreach (string keyName in keyNames) { RegistryKey rk2 = rk.OpenSubKey(keyName, true); byte[] addressBytes = (byte[])rk2.GetValue("Address"); BluetoothAddress ba = new BluetoothAddress(addressBytes); if (ba == this.DeviceAddress) { //already in there if (!state) { break; } } if (ba == BluetoothAddress.None) { if (state) { //need to add rk2.SetValue("Address", DeviceAddress.ToByteArray()); rk2.SetValue("Service", service.ToByteArray()); rk2.Close(); written = true; break; } } rk2.Close(); } if (state && !written) { RegistryKey rk2 = rk.CreateSubKey("1"); rk2.SetValue("Address", DeviceAddress.ToByteArray()); rk2.SetValue("Service", service.ToByteArray()); rk2.Close(); } rk.Close(); } } else { throw new PlatformNotSupportedException("Not supported under Windows CE"); } #else GetDeviceInfo(); // MSDN says the posible errors are: // ERROR_INVALID_PARAMETER The dwServiceFlags are invalid. // ERROR_SERVICE_DOES_NOT_EXIST The GUID specified in pGuidService is not supported. // Numerically: // #define ERROR_FILE_NOT_FOUND 2L // #define ERROR_SERVICE_DOES_NOT_EXIST 1060L // #define ERROR_NOT_FOUND 1168L // // Seen: // • 0x00000424 = 1060 ----> ERROR_SERVICE_DOES_NOT_EXIST // When service not present, or device not present. // // • 0x80070002 -/\-> ERROR_FILE_NOT_FOUND // PANU on Broadcom peer. "No driver for service"? // // • 0x00000490 = 1168 ----> ERROR_NOT_FOUND // Setting 'False' on a service not set previously registered. const int BLUETOOTH_SERVICE_DISABLE = 0x00; const int BLUETOOTH_SERVICE_ENABLE = 0x01; int hresult = NativeMethods.BluetoothSetServiceState(IntPtr.Zero, ref deviceInfo, ref service, state ? BLUETOOTH_SERVICE_ENABLE : BLUETOOTH_SERVICE_DISABLE); if (hresult != 0) { System.ComponentModel.Win32Exception ex = new System.ComponentModel.Win32Exception(hresult); if (throwOnError) { throw ex; } } #endif }
//--------- методы класса ---------// public void Save() { if (CanSave) { Microsoft.Win32.RegistryKey TempRegistryKey = this.RegistryKey; //Extended { object valueParam; bool existsParam = ParameterExist("Extended", TempRegistryKey, out valueParam); if (Extended ^ existsParam) { if (existsParam) { TempRegistryKey.DeleteValue("Extended"); } else { TempRegistryKey.SetValue("Extended", "", RegistryValueKind.String); } } } //Position { object valueParam; bool existsParam = ParameterExist("Position", TempRegistryKey, out valueParam); if (existsParam) { valueParam = ((string)valueParam).ToLower(); } if ((position == true ^ (string)valueParam == "top") || (position == null ^ existsParam == false) || (position == false ^ (string)valueParam == "bottom")) { if (position == null) { TempRegistryKey.DeleteValue("Position"); } else { TempRegistryKey.SetValue("Position", position == true ? "Top" : "Bottom", RegistryValueKind.String); } } } //MUIVerb { if (this.muiVerb != null) { this.muiVerb = this.muiVerb.Trim(); } object valueParam; bool existsParam = ParameterExist("MUIVerb", TempRegistryKey, out valueParam); if (!(muiVerb == null && existsParam == false) || !(muiVerb == (string)valueParam)) { if (muiVerb == null) { TempRegistryKey.DeleteValue("MUIVerb"); } else { TempRegistryKey.SetValue("MUIVerb", muiVerb.Trim()); } } } //Command { if ((this.isMenu == true || this.isMenu == null) && command != null) { throw new Exception("This key is 'menu', commands are not allowed."); } if (!(this.isMenu == true || this.isMenu == null)) { if (command == null) { throw new Exception("This key must have a command."); } this.command = this.command.Trim(); string[] subsection = TempRegistryKey.GetSubKeyNames(); if (subsection.Length == 1) { if (subsection[0] == "command") { RegistryKey RKCommand = TempRegistryKey.OpenSubKey("command", true); object valueParam_1; object valueParam_2; bool ByDefault_Param = ParameterExist("", RKCommand, out valueParam_1); bool DelegateExecute_Param = ParameterExist("DelegateExecute", RKCommand, out valueParam_2); if (ByDefault_Param) { if (this.command != (string)valueParam_1) { RKCommand.SetValue("", this.command); } } else if (DelegateExecute_Param) { if (this.command != (string)valueParam_2) { RKCommand.SetValue("DelegateExecute_Param", this.command); } } else { throw new Exception("Сommand not detected."); } } else { throw new Exception("The 'command' subsection was not detected."); } } else if (subsection.Length == 0) { throw new Exception("The 'command' subsection was not detected."); } } } //RegistryName { if (this.registryName.Length == 0) { throw new MyProgramException("Имя в реестре не может быть пустым.", TypeException.NotFatal); } this.registryName = this.registryName.Trim(); Microsoft.Win32.RegistryKey tmpParent = this.ParentRegistryKey; string tmpRKeyName = this.FullNameRegistryKey; //получение имени ключа for (int i = tmpRKeyName.Length - 1; i > 0; i--) { if (tmpRKeyName[i] == '\\') { tmpRKeyName = tmpRKeyName.Substring(i + 1, tmpRKeyName.Length - i - 1); break; } } if (this.registryName != tmpRKeyName) { string[] sub_names = tmpParent.GetSubKeyNames(); if (IsUniqueName(ref sub_names, this.registryName)) { Microsoft.Win32.RegistryKey target = tmpParent.CreateSubKey(this.registryName); CopyRegistryKey(this.RegistryKey, target); tmpParent.DeleteSubKeyTree(tmpRKeyName); this.FullNameRegistryKey = target.Name; } else { throw new Exception("Не уникльное имя."); } } } canSave = false; } }
private static void DeleteSubKeyTree(RegistryKey key, string subKey) { try { key.DeleteSubKeyTree(subKey); } catch (ArgumentException) { // Eat exception in case subkey does not exist. } }
void QuietDeleteSubKeyTree(RegistryKey key, string name){ RegistryKey subKey = key.OpenSubKey(name,true); if (subKey != null) { subKey.Close(); key.DeleteSubKeyTree(name); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void Unregister (RegistryKey rootKey) { rootKey.DeleteSubKeyTree (CLSIDKey); }
/// <summary> /// Deletes one key from the registry. /// </summary> /// <param name="root">The root node to delete the key under.</param> /// <param name="name">The path to the key to delete under the root node.</param> private static void DeleteKey(RegistryKey root, string name) { Param.AssertNotNull(root, "root"); Param.AssertValidString(name, "name"); try { int index = name.LastIndexOf("\\", StringComparison.Ordinal); if (-1 == index) { root.DeleteSubKeyTree(name); } else { string delete = name.Substring(index + 1, name.Length - index - 1); PathInfo pathinfo = RegistryUtils.GetPath(root, name); if (pathinfo.Key != null) { pathinfo.Key.DeleteSubKeyTree(delete); } } } catch (ArgumentException) { // This happens when the key does not exist. Just ignore it. } catch (SecurityException) { } }