2016-11-19 24 views
-1

我與此代碼有衝突問題,它仍然想要刪除值「StateFlags0200」或「StateFlags0300」,即使它不存在於Windows註冊表,但我的邏輯似乎對我來說是正確的。我與此代碼有衝突問題,它仍然想要刪除值

enter code here 

私人無效checkBox1_CheckedChanged(對象發件人,EventArgs的)

{ 
     #region Block #1 

     if ((checkBox1.Checked == true) & (radioButton1.Checked == true)) 
     { 
      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.SetValue("StateFlags0100", Convert.ToInt32("2", 16), RegistryValueKind.DWord); 
      KeyPath.Close(); 

     } 
     else 
     { 

      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.DeleteValue("StateFlags0100"); 
      KeyPath.Close(); 
     } 

     if ((checkBox1.Checked == true) & (radioButton2.Checked == true)) 
     { 
      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.SetValue("StateFlags0200", Convert.ToInt32("2", 16), RegistryValueKind.DWord); 
      KeyPath.Close(); 

     } 
     else 
     { 

      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.DeleteValue("StateFlags0200"); 
      KeyPath.Close(); 
     } 

     if ((checkBox1.Checked == true) & (radioButton3.Checked == true)) 
     { 
      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.SetValue("StateFlags0300", Convert.ToInt32("2", 16), RegistryValueKind.DWord); 
      KeyPath.Close(); 

     } 
     else 
     { 

      RegistryKey KeyBase = Registry.LocalMachine; 
      RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
      KeyPath.DeleteValue("StateFlags0300"); 
      KeyPath.Close(); 
     } 
     #endregion 
    } 

回答

0

你需要做的是這樣的:

RegistryKey KeyBase = Registry.LocalMachine; 
RegistryKey KeyPath = KeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", true); 
KeyPath.DeleteValue("StateFlags0100", false); 
KeyPath.Close(); 

爲DeleteValue的默認行爲是拋出如果該值未找到。爲了改變這種情況,該方法的第二個版本存在,它接受第二個布爾參數,該參數明確說明如果該值不存在則拋出異常。更多信息:https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.deletevalue(v=vs.110).aspx

+0

它可以工作,但當我從radiobutton1更改爲另一個時,它將刪除初始值。這些是我試圖製作的配置文件。在註冊表中,它不應該刪除其他「StateFlags」。 –

+0

簡單的解決方案是創建3個選項卡,並將我的複選框與代碼中的相同名稱相同的三個複選框相加,然後刪除我的單選按鈕。會有另一種解決方案嗎? 對我英語不好的藉口。 –

+0

它如何刪除「初始值」?我無法從上面的代碼中看到這是可能的。 –