2011-08-29 73 views
36

我試圖用我的C#應用​​程序寫入註冊表。在C#應用程序中寫入註冊表

我使用這裏給出了答案:Writing values to the registry with C#

但是由於某種原因,關鍵是不能添加到註冊表中。

我用下面的代碼:

string Timestamp = DateTime.Now.ToString("dd-MM-yyyy"); 

string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\"+Application.ProductName+"\\"+Application.ProductVersion; 
string valueName = "Trial Period"; 

Microsoft.Win32.Registry.SetValue(key, valueName, Timestamp, Microsoft.Win32.RegistryValueKind.String); 

Application.nameApplication.version '文件夾' 不存在呢。

我必須先創建它們嗎?

此外,我在64b Win版本上測試它,所以我認爲如果我想檢查註冊表中添加的密鑰,我必須專門檢查32位註冊表:C:\ Windows \ SysWOW64 \ regedit.exe不是嗎?

+2

UAC將破壞您的計劃,您無法在沒有提升的情況下向HKLM寫信。除非你編寫了一個改變密鑰可訪問性的安裝程序。許可證實施代碼是您購買的代碼種類。花一分錢賺一分錢。 –

+0

你應該使用boxedapp。它必須幫助你。 –

回答

57

首先,如果你想編輯LOCALMACHINE下鍵,您必須運行在管理員權限您的應用程序(更好地利用CurrentUser它的安全或創建安裝程序中的關鍵)。您還必須在編輯模式下打開密鑰(OpenSubKey方法)才能添加新的子密鑰。我已經檢查了代碼並且它可以工作。這是代碼。

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software",true); 

key.CreateSubKey("AppName"); 
key = key.OpenSubKey("AppName", true); 


key.CreateSubKey("AppVersion"); 
key = key.OpenSubKey("AppVersion", true); 

key.SetValue("yourkey", "yourvalue"); 
+2

將數據寫入用戶文件夾 - Microsoft.Win32.Registry.CurrentUser - 而不是本地計算機 - Microsoft.Win32.Registry.LocalMachine更安全。 http://msdn.microsoft.com/en-us/library/h5e7chcf.aspx –

+0

我試着做一些基本相同的事情,但使用'using'方法。當密鑰爲空時,會導致一些奇怪的問題,所以我嘗試了這種方法,而不會因爲空密鑰異常而觸發問題。快樂的一天! ^^ –

1

嘗試首先打開HKLM\Software。然後爲您的程序創建密鑰,然後爲版本創建密鑰。但是,您的密鑰可以放在HKLM \ software \ WOW6432Node中。檢查這個。

6

另請檢查您的註冊表調用是否正在虛擬化。有關更多信息,請參閱here

如果您的應用程序不是UAC aware並且出於兼容性原因而出現,則會發生這種情況。所有的

Real path 
HKEY_LOCAL_MACHINE\Software\FooKey 

Virtual path 
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\FooKey 
3

您可以使用下面的代碼來創建並打開所需的註冊表項。

RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("Software",true); 

RegistryKey AppNameKey = SoftwareKey.CreateSubKey("AppName"); 
RegistryKey AppVersionKey = AppNameKey.CreateSubKey("AppVersion"); 

AppVersionKey.SetValue("yourkey", "yourvalue"); 

基本上可以使用CreateSubKey你所有的應用程序設置,因爲它會打開寫訪問的關鍵,如果它已經存在,否則創建它。沒有必要先創建,然後打開。 OpenSubKey就派上用場了,當你完全確定鍵已經存在,像在這種情況下,「HKEY_LOCAL_MACHINE \ SOFTWARE \」

-3

謝謝大家對他們的意見和幫助

下面是我想出了最後的工作液用。它與其他類似的收藏集成在一起。我最初使用了一個列表,但我需要一個字符串作爲其他收藏的鑰匙

// ************************** Ordered Dictionary - works **************** 
// http://stackoverflow.com/questions/2722767/c-sharp-order-preserving-data-structures 
// http://www.go4expert.com/articles/understanding-c-sharp-dictionaries-t30034/ 

public OrderedDictionary m_oCol; 
public OrderedDictionary m_oColReverse; 

public clsFeatureCollection() 
    : base() 
{ 
    m_oCol = new OrderedDictionary(); 
    m_oColReverse = new OrderedDictionary(); 
} 

public IEnumerator GetEnumerator() 
{ 
    return m_oCol.GetEnumerator(); 
} 

public void Add(IFeature pFeature, string strBefore = "", string strAfter = "", bool bReverse = false) 
{ 
    if (bReverse == true) 
    { 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()); 
    } 

    if (!ContainsItem(pFeature.OID.ToString())) 
    { 
     m_oCol.Add(pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy)); 
    } 
} 

public void AddBefore(IFeature pFeature, string strBefore, bool bReverse = false) 
{ 
    if (bReverse == true) 
    { 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()); 
    } 

    if (!ContainsItem(pFeature.OID.ToString())) 
    { 
     if (strBefore != null) 
     { 
      int index = GetIndex(m_oCol, strBefore); 

      if (index > 0) 
      { 
       m_oCol.Insert(index - 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy)); 

      } 
      else 
      { 
       m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy)); 
      } 
     } 
    } 
} 

public void AddAfter(IFeature pFeature, string strAfter, bool bReverse = false) 
{ 
    if (bReverse == true) 
    { 
     m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim()); 
    } 

    if (!ContainsItem(pFeature.OID.ToString())) 
    { 
     if (!string.IsNullOrEmpty(strAfter)) 
     { 
      int index = GetIndex(m_oCol, strAfter); 

      m_oCol.Insert(index + 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy)); 
     } 
     else 
     { 
      m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy)); 
     } 
    } 
} 

public int Count 
{ 
    get { return m_oCol.Count; } 
} 

public void Remove(int Id) 
{ 
    m_oCol.RemoveAt(Id); 
} 

public clsFeature Item(int Position) 
{ 
    try 
    { 
     clsFeature value = (clsFeature)m_oCol.Cast<DictionaryEntry>().ElementAt(Position).Value; 

     return value; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

public void Clear() 
{ 
    m_oCol = new OrderedDictionary(); 
    m_oColReverse = new OrderedDictionary(); 
} 

public bool Reverse(string valueRenamed) 
{ 
    bool bReverse = false; 

    try 
    { 
     if (m_oColReverse.Contains(valueRenamed)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    catch (Exception ex) 
    { 
     if (ex is ArgumentException | ex is IndexOutOfRangeException) 
     { 
      bReverse = false; 
     } 
    } 

    return bReverse; 
} 

public bool ContainsItem(string oidValue) 
{ 
    bool bContainsItem = false; 

    string intOID = oidValue.ToString(); 

    try 
    { 
     // dictionary 
     if (m_oCol.Contains(intOID)) 
     { 
      bContainsItem = true; 
     } 
     else 
     { 
      bContainsItem = false; 
     } 

     return bContainsItem; 
    } 

    catch (Exception ex) 
    { 
     if (ex is ArgumentException | ex is IndexOutOfRangeException) 
     { 
      bContainsItem = false; 
     } 
    } 

    return bContainsItem; 
} 

public static int GetIndex(OrderedDictionary dictionary, string key) 
{ 
    for (int index = 0; index < dictionary.Count; index++) 
    { 
     if (dictionary[index] == dictionary[key]) 
     { 
      return index; 
     } 
    } 

    return -1; 
} 

// ****************************** End Ordered Dictionary - works ************************ 
+7

這是如何回答這個問題? –

1

問題是您沒有足夠的特權。下面是我的工作方式:

RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); 
myKey = myKey.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl); 

if (myKey != null) 
{ 
    myKey.SetValue("DefaultPrinterId", ldiPrinters[e.RowIndex].id, RegistryValueKind.String); 
    myKey.Close(); 
} 

隨着RegistryKey.OpenBaseKey打開正確的註冊表,因爲當你沒有權限,你寫註冊表,它在另一個位置。