2009-11-04 12 views
20

私はbinフォルダからDLLをインポートしているWebアプリケーションを持っています。レジストリキーを読む

const string dllpath = "Utility.dll"; 

    [DllImport(dllpath)] 

私がしたいことは、まず現在のプロジェクトにはないフォルダから別の場所にDLLをインポートすることです。

そのフォルダのパスはレジストリキーに格納されます。

どうすればよいですか?

編集

私はこれをうまくできないのはなぜ?

public partial class Reports1 : System.Web.UI.Page 
{ 

    RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\xyz"); 
    string pathName = (string)registryKey.GetValue("BinDir"); 

    const string dllpath = pathName; 
    [DllImport(dllpath)] 
    public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

string pathName = (string)registryKey.GetValue("BinDir");私はこれをどのように修正することができます... ここで働いていないが、pageloadイベントで働いている...

しかし、私がしなければ、このDLLのインポートは動作しませんか?

答えて

43

レジストリを読むのはかなり簡単です。 Microsoft.Win32名前空間の静的クラスはRegistryです。 HKLMノードからキーを読み取るには、コードは次のとおりです。

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\NodeName") 

ノードがHKCUある場合は、CurrentUserLocalMachineを置き換えることができます。

RegistryKeyオブジェクトを取得したら、GetValueを使用してレジストリから値を取得します。パス名のレジストリ値は次のようになり得て、上記の例を使用し続けて:

string pathName = (string) registryKey.GetValue("pathName"); 

そしてUsingブロックに値を取得するには、あなたがそれで行われたときにRegistryKeyオブジェクトを閉じる(またはステートメントを置くことを忘れないでください)。

アップデート

私は物事のカップルを参照してください。まず、私が定義された静的プロパティことへのパス名を変更すると同じように2つの問題があっ

Private static string PathName 
{ 
    get 
    { 
     using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\Copium")) 
     { 
       return (string)registryKey.GetValue("BinDir"); 
     } 
    } 
} 

  1. RegistryKey参照が開いたレジストリを維持します。これをクラスの静的変数として使用すると、コンピュータに問題が発生します。
  2. レジストリパスは、スラッシュではなく、スラッシュを使用します。
2
try 
{ 
    RegistryKey regKey = Registry.LocalMachine; 
    regKey = regKey.OpenSubKey(@"Software\Application\"); 

    if (regKey != null) 
    { 
     return regKey.GetValue("KEY NAME").ToString(); 
    } 
    else 
    { 
     return null; 
    } 
} 
catch (Exception ex) 
{ 
    return null; 
} 
1

あなたはこれを使用することができます:

/// <summary> 
/// To read a registry key. 
/// input: KeyName (string) 
/// output: value (string) 
/// </summary> 
public string Read(string KeyName) 
{ 
    // Opening the registry key 
    RegistryKey rk = baseRegistryKey ; 
    // Open a subKey as read-only 
    RegistryKey sk1 = rk.OpenSubKey(subKey); 
    // If the RegistrySubKey doesn't exist -> (null) 
    if (sk1 == null) 
    { 
     return null; 
    } 
    else 
    { 
     try 
     { 
      // If the RegistryKey exists I get its value 
      // or null is returned. 
      return (string)sk1.GetValue(KeyName.ToUpper()); 
     } 
     catch (Exception e) 
     { 
      // AAAAAAAAAAARGH, an error! 
      ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper()); 
      return null; 
     } 
    } 
} 

をより多くの情報訪問this web siteについて。

8

これらの回答はどれも私のためには役に立たなかった。これは私が使用したものです:

static void Main() 
{ 
    const string dotNetFourPath = "Software\\Microsoft";//note backslash 
    using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(dotNetFourPath)) 
    { 
     Console.WriteLine(registryKey.SubKeyCount);//registry is not null 
     foreach (var VARIABLE in registryKey.GetSubKeyNames()) 
     { 
      Console.WriteLine(VARIABLE);//here I can see I have many keys 
      //no need to switch to x64 as suggested on other posts 
     } 
    } 
} 
2

これらのすべての答えは、今日の通常の64ビットOS上で実行すると問題につながる可能性があります。

私の状況では、私は「Any CPU」ターゲットにコンパイルして、64bit OSにインストールするとうまく動作しています。 しかし私の単体テストは問題を抱えています。明らかに32ビットモードで実行されています。

この場合、HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MySoftwareは検索されませんが、HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\MyCompany\MySoftwareは入力されていません。我々は我々が使用することができ、合計で

RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) 

を使用してGoogleの検索の開始点を指定する必要があります。このような状況で

string configurationDirectory = string.Empty; 

using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) 
{ 
    using (RegistryKey registryKey = hklm.OpenSubKey(@"SOFTWARE\MyCompany\MySoftware")) 
    { 
     if (registryKey != null) 
     { 
      configurationDirectory = (string)registryKey.GetValue("ConfigurationDirectory"); 
     } 
    } 
}