2011-08-29 19 views
36

C#アプリケーションを使用してレジストリに書き込もうとしています。C#アプリケーションのレジストリへの書き込み

Writing values to the registry with C#は、しかし、いくつかの理由でキーがレジストリに追加されていません。

私はここに与えられた答えを使用しています。

私は、次のコードを使用しています:Application.nameApplication.version 'フォルダ' はまだ存在しません

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); 

を。

最初に作成する必要はありますか?

また、私は64b Win版でテストしていますので、キーのレジストリを確認したい場合は、32bitレジストリを確認する必要があります。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.LocalMachineというユーザーフォルダ(Microsoft.Win32.Registry.CurrentUser)にデータを書き込む方が安全です。 http://msdn.microsoft.com/en-us/library/h5e7chcf.aspx –

+0

私は基本的に同じことをやってみましたが、 'using'方法論を使用しました。キーがnullだった場合、それはいくつかの奇妙な問題を引き起こしました。そのため、nullキーの例外のために問題を引き起こさずにこれを試しました。幸せな一日! ^^ –

1

最初にHKLM\Softwareを開こうとします。次に、プログラムのキーを作成し、バージョンのキーを作成します。 Howewer、あなたの鍵は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であなたが書くレジストリのアクセス許可がない場合、それは別の場所にないので、あなたは、正しいレジストリを開きます。

関連する問題