2011-08-29 74 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打开正确的注册表,因为当你没有权限,你写注册表,它在另一个位置。