简体   繁体   English

将dll存储在与bin不在同一项目中的另一个文件夹中

[英]store dll in another folder not in same project other than bin

ok... now this is something new for me.. 好吧...现在这对我来说是新事物。

I have a utility.dll file which is in my bin folder and i am accessing it in my current application. 我在bin文件夹中有一个utility.dll文件,我正在当前应用程序中访问它。 This part is working fine.... 这部分工作正常。

public partial class Reports1 : System.Web.UI.Page
{
[DllImport("Utility.dll")]
    public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize);
}

But now i have to use .dll from a folder not in the bin but some other folder in the C:/ 但是现在我必须使用不在文件夹中的文件夹中的.dll,而是在C:/中的其他文件夹中使用

I tried using registery key in which i stored the path of the folder in a registery key and get that path and put it in place of Utility.dll but this did not work.... I got an error message An attribute must be a constant expression of an attribute parameter type.... 我尝试使用注册密钥,在该密钥中,我将文件夹的路径存储在注册密钥中,并获取该路径并将其放置在Utility.dll的位置,但这不起作用。...我收到一条错误消息,属性必须为属性参数类型的常量表达式。

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

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


    [DllImport(PathName)]
    public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize);

Naybody with any suggestions... Thanks. Naybody有任何建议...谢谢。

Could you use an environmental variable as part of the DllImport string argument constant? 您能否将环境变量用作DllImport字符串参数常量的一部分? That way you can "parameterize" it from outside the application. 这样,您可以从应用程序外部对其进行“参数化”。

Also, it might be that as long as the DLL file is somewhere in the current execution scope's PATH (environmental path, not file path). 另外,可能是只要DLL文件位于当前执行范围的PATH(环境路径,而不是文件路径)中。 (C:\\windows\\system32 for example.) (例如,C:\\ windows \\ system32。)

If it's a DLL that you are going to be using in several different programs you could register the DLL in the Global Assembly Cache. 如果您要在几个不同的程序中使用的是DLL,则可以在全局程序集缓存中注册该DLL。 See here 这里

Check out this article to see the order that the loader searches for DLL's. 查看本文以查看加载程序搜索DLL的顺序。

You can also add your own path to the search folder using SetDllDirectory , but that probably won't work with DLLImport . 您也可以使用SetDllDirectory将自己的路径添加到搜索文件夹中,但是DLLImport可能DLLImport

Possibly the Value property of the DllImport attribute sheds some light on this when it states 可能,DllImport属性的Value属性在声明时对此有所说明

You can provide a full or relative path. 您可以提供完整或相对路径。 If you provide no path, the DLL must be in the current path at run time, unless the DLL is loaded by some other means. 如果您不提供任何路径,则除非运行DLL通过其他方式加载,否则DLL必须在运行时位于当前路径中。 Be aware, however, that using a fully qualified path can introduce inaccuracy if the DLL is moved. 但是请注意,如果移动DLL,则使用完全限定的路径可能会导致不准确。

However seeing the property is read-only I have to assume it's referring to path allowances for the attribute's construction in which case you would try 但是,看到该属性是只读的,我必须假定它是指属性构造的路径余量,在这种情况下,您可以尝试

[DllImport(@"C:\mydir\yourdir\theirdir\Utility.dll")]

This is just a shot in the dark because I have not tried it. 这只是在黑暗中拍摄,因为我还没有尝试过。

Edit: And wherever that DLL is sitting, make sure it has the correct file permissions to be used. 编辑:无论该DLL位于何处,请确保它具有要使用的正确文件权限。

Another way of accomplishing this is to perhaps use a combination of AppDomain.AssemblyResolve event and the Assembly.LoadFrom() method as noted here: http://codebetter.com/blogs/patricksmacchia/archive/2007/07/19/prevent-corrupted-installation-in-production.aspx 完成此操作的另一种方法可能是结合使用AppDomain.AssemblyResolve事件和Assembly.LoadFrom()方法,如此处所述: http : //codebetter.com/blogs/patricksmacchia/archive/2007/07/19/prevent-生产中损坏的安装.aspx

I personally haven't tried this out, but it looks and sounds like it's work for you. 我个人还没有尝试过,但是看起来和听起来像是对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个文件夹而不是bin文件夹包括DocumentFormat.OpenXml.dll的引用 - How to include the reference of DocumentFormat.OpenXml.dll from another folder rather than bin folder 从bin以外的文件夹加载程序集 - Load Assemblies from a folder other than bin 如何从dll项目内的bin文件夹中获取文件夹位置? - How to get a folder location from the bin folder inside a dll project? 如何访问ASP.NET MVC项目的bin文件夹中的DLL? - How to access DLL in bin folder of ASP.NET MVC project? 自动将本机dll复制到Visual Studio中引用项目的bin文件夹中 - Automaticaly copy native dll to the bin folder of referencing project in Visual Studio 数据访问项目dll未添加到客户端bin文件夹中 - Data Access project dll is not being added to the client bin folder 为什么将dll从GAC复制到project \\ bin文件夹 - Why a dll is copied from GAC to project\bin folder 相同的旧dll覆盖bin文件夹中的现有较新版本 - same older dll override existing newer version in bin folder 多个项目中的相同dll,并将dll与exe保留在同一文件夹中 - same dll in multiple project and keeping exes with dll in same folder 同一解决方案中其他项目引用的Copylocal dll没有复制到输出文件夹中 - Referenced Copylocal dll from other project in same solution not copied into output folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM