23

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");ここでは機能していませんが、ページロードイベントで機能しています...

しかし、この DLL インポートを行うと機能しません... どうすれば修正できますか?

4

5 に答える 5

9

これらの答えはどれも私にとってはうまくいきませんでした。これは私が使用したものです:

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
        }
    }
}
于 2013-03-25T20:19:21.160 に答える
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;
}
于 2009-11-04T19:12:22.313 に答える
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;
        }
    }
}

詳細については、このWebサイトにアクセスしてください。

于 2013-02-05T15:39:10.353 に答える