Error adding 7z.dll reference to C# project

Jul 21, 2009 at 1:09 PM

Morning folks.  Beginner C# programmer here.  I've installed SevenZipSharp successfully, added SevenZipZharp.dll as a reference in my project, added 7z.dll to my project's /bin/Debug/ directory, and everything is working great.  In my sample project, I'm able to package and extract files with no problems.

However, I need to distribute this application to multiple users, whose PCs do not (and will not) have 7-zip and SevenZipSharp installed.  When I attempt to add 7z.dll as a reference in my project, I get an error (text below).  Since 7z.dll is only in the /bin/Debug/ directory, it is not included in the assembly when I deploy it.

Error: "A reference to 'C:\Program Files\7-Zip\7z.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assemply or COM component."

After reading the "Important Update" here, I believed that the latest version (4.65) would allow me to use the 7z.dll directly in my project.  I was mistaken.  I get the same error message.

So, then, how do I add the 7z.dll to my C# project as a reference (not just sitting in the /bin/Debug/ directory)?

Any help is appreciated.  Thanks.

 

 

Coordinator
Jul 21, 2009 at 7:32 PM

Hi, josef77.

Consider the following tips:

1)You may find useful these methods: SevenZipExtractor.SetLibraryPath("path to 7z.dll")/SevenZipCompressor.SetLibraryPath("path to 7z.dll"). So you just deploy 7z.dll so some known place and then set library path.

2)You may add 7z.dll to your project (not References but just file tree in Solution Explorer). Then check this file properties, set "Build Action" to "None", "Copy To Output" to "Copy If Newer". Now you will be sure 7z.dll will be in the same directory as your assembly, so you don't need to copy it yourself.

3)If you aim to deploy just the single file, try the following trick:

add 7z.dll to your project (just like in the 2), set "Build Action" to "Embedded Resource". Then edit Program.cs:

...
static void Main(string[] args)
{
    if (!File.Exists("7z.dll"))
    {
        using (Stream szdllStream =
            System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("<your assembly name>.7z.dll"))
        {
            using (FileStream fs = File.Create("7z.dll"))
            {
                int bufSize = 1024;
                byte[] buf = new byte[bufSize];
                int cnt;
                while ((cnt = szdllStream.Read(buf, 0, bufSize)) > 0)
                {
                    fs.Write(buf, 0, cnt);
                }
            }
        }
    }
...

The point is to embed 7z.dll as a resource, and extract it when necessary.

Hope it will help. Good luck.