11

iv tried numerous different ways to get this to work and i got it to basicly work but i cant get the WaitForExit(); 's to work like they do here... so how would i convert this to work with sevenzip? cause i cant get it to work, and also i the SFX's are passworded so that they cannot be accessed except by the use of the program and as for adding the 7z.DLL i cant add it in because i get the error:

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

string tempFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
System.Diagnostics.Process defrag1 = System.Diagnostics.Process.Start(@"AusLogics_Defrag.exe", string.Format(" -o{0} -y -Pthisisthepass", tempFolder));
defrag1.WaitForExit();
string executableDirectoryName = Path.GetDirectoryName(Application.ExecutablePath);
System.Diagnostics.Process defrag2 = System.Diagnostics.Process.Start(tempFolder + "\\" + "AusLogics_Defrag" + "\\" + "DiskDefrag.exe", "");
defrag2.WaitForExit();
System.IO.Directory.Delete(tempFolder + "\\" + "AusLogics_Defrag", true);

new: ok well this is what i have so far but im getting that error "Can not load 7-zip library or internal COM error! Message: failed to load library"

SevenZipExtractor.SetLibraryPath("7z.dll"); //no idea of this is needed or not
SevenZipCompressor.SetLibraryPath("7z.dll"); //no idea of this is needed or not
string tempFolder = Environment.GerFolderPath(Environment.SpecialFolder.ApplicationData);
SevenZipExtractor defrag = new SevenZipExtractor(@"Programs\Optimize\Auslogics_Defrag.7z");
defrag.ExtracArchive(string.Format("-o{0} -y -PThisisthepass", tempFolder));
NightsEVil
  • 507
  • 2
  • 13
  • 21
  • Are you sure you are trying to add a reference to a .NET component and not a regular dll? Also, what has the code fragment you posted got to do with the question? – Oded Jun 24 '10 at 08:16
  • well im curious how i can use sevenzipsharps commands but still use the "WaitForExit();" 's cause it couldnt do it when i tried it – NightsEVil Jun 24 '10 at 08:29

3 Answers3

23

Reference SevenZipSharp.dll from you .NET project and make sure you have the 7z DLL copied to the target output directory as a post-build event. Since 7z.dll is not a .NET assembly your .NET project cannot reference it directly.

There are two typical explanations for "Can not load 7-zip library or internal COM error! Message: failed to load library":

  1. The obvious is that the 7z DLL can't be found. In that case call SevenZipCompressor/SevenZipExtractor.SetLibraryPath() with the full path of the DLL before making any related SevenZipSharp calls. Relative paths should work as well but try the absolute in case some code changed the current directory of the process. One strategy to get the absolute path is to use the path of the executing assembly, see below for an example.

  2. The architecture of the DLL being referenced doesn't match the current process. For example your .NET assembly is running x64 but you reference a 32-bit version of 7z.dll. In that case you would need to reference 7z64.dll. Download the 7-Zip DLL binaries from SevenZipSharp's release to ensure there are no other mismatch issues and make sure you're using the correct version.

Here is an example how to set the absolute path of the 7z.dll if it's in the same directory as the assembly using it:

SevenZip.SevenZipCompressor.SetLibraryPath(
    Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),  
        "7z.dll"));
Scott Lerch
  • 2,620
  • 1
  • 23
  • 34
  • 3
    +1 Worked for me, but note that the call to SetLibraryPath must come before you instantiate the SevenZipExtractor / SevenZipCompressor – 79E09796 Jul 24 '12 at 11:48
1

You need to add a reference to SevenZipSharp.dll, not to the regular 7za.dll or the 7z.dll.

Since you need to have 7zip dlls available, you need to package them with your code - that does not mean you need to reference them.

You can add a solution/project folder to your application and add the required dll's there. Make sure that you set their the "Copy To Build Directory" property to "Copy if Newer".

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • "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" but im in visual studio 2010express and i dont see a libraryManager.cs – NightsEVil Jun 24 '10 at 08:22
  • @NightsEVil - `LibraryManager.cs` is a `SevenZipSharp` source file. http://sevenzipsharp.codeplex.com/SourceControl/changeset/view/49277#364698 – Oded Jun 24 '10 at 08:28
  • i keep getting "Can not load 7-zip library or internal COM error! Message: failed to load library" .. why? – NightsEVil Jun 24 '10 at 08:53
  • @NightsEVil - because `SevenZipSharp` needs to have access to the 7zip dlls. You can add a configuration app setting ("7zLocation") so it knows where they are located. – Oded Jun 24 '10 at 09:02
  • ok well its ment to be on a flash drive or a cd so its not going to always have the same drive letter so can i just do a relative path? – NightsEVil Jun 24 '10 at 09:06
1

If you're using the 32-bit version of the .dll, you can try setting your project to prefer 32-bit architecture.

  - From project properties...   Build  >  check "Prefer 32-bit"