A reference to .dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component

≯℡__Kan透↙ 提交于 2019-12-11 09:37:08

问题


Ok now i will try to explain my problem as much as possible. I want to use popular compression algorithm 7zip at my c# project

There is already NuGet package for 7zip

Now when i install 7zip page https://www.nuget.org/packages/SevenZipSharp/0.64.0 it installs fine however it gives error when i try to run

An unhandled exception of type 'SevenZip.SevenZipLibraryException' occurred in SevenZipSharp.dll

Additional information: Can not load 7-zip library or internal COM error! Message: failed to load library

So i decide to add dll file manually and i get this error below

    ---------------------------
Microsoft Visual Studio
---------------------------
A reference to 'D:\51_doktora tez projesi\program_crawler\doktora_tez_projesi_crawler_program\ExternalDLLs\7z_9_38_2015_01_03.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
---------------------------
OK   
---------------------------

Ok but i found a solution and it works that way

First install nuget package

And before start calling functions set path

SevenZip.SevenZipExtractor.SetLibraryPath("SevenZipSharp.dll");

SevenZip.SevenZipCompressor.SetLibraryPath("SevenZipSharp.dll");

Ok second question which is also interesting

I wanted to use latest version of 7zip DLL file. So downloaded it from official website : https://sourceforge.net/projects/sevenzip/files/7-Zip/9.38/

Downloaded MSI installed and got 64bit dll file. However when i try to reference this file, it fails with error message above: Please make sure that the file is accessible, and that it is a valid assembly or COM component.

However the trick works again

I install NuGet 7zip package. Then before calling functions i set these 2 lines of code and it uses latest version of DLL file

SevenZip.SevenZipExtractor.SetLibraryPath("ExternalDLLs/7z_9_38_2015_01_03.dll");
            SevenZip.SevenZipCompressor.SetLibraryPath("ExternalDLLs/7z_9_38_2015_01_03.dll");

So i am looking for answers why all these happens? Why i can not directly add as a reference the DLL file however the trick works?

windows 8.1 64bit, Visual Studio 2013 update 3 WPF application


回答1:


Part of your problem most likely stems from the fact that SevenZipSharp is merely a wrapper for the 7z.dll, which to the best of my knowledge is a c++ dll. The project page, for SevenZipSharp, also mentions that any compatible dll can be used and needs to be specified:

SevenZipSharp requires a 7-zip native library to function. You can specify the path to a 7-zip dll (7z.dll, 7za.dll, etc.) in LibraryManager.cs at compile time, your app.config or via SetLibraryPath() method at runtime. + "7z.dll" is the default path. For 64-bit systems, you must use the 64-bit versions of those libraries. 7-zip ships with 7z.dll, which is used for all archive operations (usually it is "Program Files\7-Zip\7z.dll"). 7za.dll is a light version of 7z.dll, it supports only 7zip archives. You may even build your own library with formats you want from 7-zip sources. SevenZipSharp will work with them all.



来源:https://stackoverflow.com/questions/29424657/a-reference-to-dll-could-not-be-added-please-make-sure-that-the-file-is-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!