我如何解决“请确保该文件是可访问的,并且它是一个有效的程序集或 COM 组件”?

我正在用 C # 中的 OpenCV 构建一个项目。它需要一个名为 cvextern.dll的 dll 文件。但是,当添加此文件作为引用时,会出现以下消息:-

无法添加引用“ cvater.dll”,请确保该文件可访问,并且它是有效的程序集或 COM 组件。

我在过去的两天里一直在寻找解决这个问题的方法

154109 次浏览

the file is a native DLL which means you can't add it to a .NET project via Add Reference... you can use it via DllImport (see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx)

Make sure the required dlls are exported (or copied manually) to the bin folder when building your application.

In my case I had to register the .dll.

To do so, open cmd.exe (the console) with admin rights and type:

regsvr32 "foo.dll"

I had the same program, I hope this could help.

I your using Windows 7, open Command Prompt-> run as Administrator. register your <...>.dll.

Why run as Administrator, you can register your <...>.dll using the run at the Windows Start, but still your dll only run as user even your account is administrator.

Now you can add your <...>.dll at the Project->Add Reference->Browse

Thanks

Look here for the answer by TheMattster. I implemented it and it worked like a charm. In a nutshell, his solution suggests to add the COM dll as a resource to the project (so now it compiles into the project's dll), and upon the first run write it to a file (i.e. the dll file I wanted there in the first place).

The following is taken from his answer.

Step 1) Add the DLL as a resource (below as "Resources.DllFile"). To do this open project properties, select the resources tab, select "add existing file" and add the DLL as a resource.

Step 2) Add the name of the DLL as a string resource (below as "Resources.DllName").

Step 3) Add this code to your main form-load:

if (!File.Exists(Properties.Resources.DllName))
{
var outStream = new StreamWriter(Properties.Resources.DllName, false);
var binStream = new BinaryWriter(outStream.BaseStream);
binStream.Write(Properties.Resources.DllFile);
binStream.Close();
}

My problem was that not only I had to use the COM dll in my project, I also had to deploy it with my app using ClickOnce, and without being able to add reference to it in my project the above solution is practically the only one that worked.

'It' requires a dll file called cvextern.dll . 'It' can be either your own cs file or some other third party dll which you are using in your project.

To call native dlls to your own cs file, copy the dll into your project's root\lib directory and add it as an existing item. (Add -Existing item) and use Dllimport with correct location.

For third party , copy the native library to the folder where the third party library resides and add it as an existing item.

After building make sure that the required dlls are appearing in Build folder. In some cases it may not appear or get replaced in Build folder. Delete the Build folder manually and build again.

In my case I also have unmanaged dll's (C++) in workspace and if you specify:

<files>
<file src="bin\*.dll" target="lib" />
</files>

nuget would try to load every dll as an assembly, even the C++ libraries! To avoid this behaviour explicitly define your C# assemblies with references tag:

<references>
<reference file="Managed1.dll" />
<reference file="Managed2.dll" />
</references>

Remark: parent of references is metadata -> according to documentation https://learn.microsoft.com/en-us/nuget/reference/nuspec#general-form-and-schema

Documentation: https://learn.microsoft.com/en-us/nuget/reference/nuspec