0

I don't have .NET 4.5 to unzip zip files, so I'm using Shell32 like this. But when I reference the dll like this:

csc.exe /r:"C:\Windows\System32\shell32.dll" ...

I get this error:

fatal error CS0009: Metadata file 'c:\Windows\System32\shell32.dll' could not be opened -- 'An attempt was made to load
    a program with an incorrect format. '

Update: Without Visual Studio, just csc.exe.

Community
  • 1
  • 1
  • You could also use `Process.Start` to run some kind of `unzip.exe` bundled with your application. Windows zipper is imperfect, so you might encounter issue with more exotic packages. – PTwr Aug 20 '16 at 13:02
  • Oh, since .NET 2.0 you can use [GZipStream](https://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream(v=vs.110).aspx), but from what I remember it have some bugs. – PTwr Aug 20 '16 at 13:11
  • @PTwr That's what I've been doing so far `Process.Start(@"cmd.ex /C powershell.exe -nologo -noprofile -command ""& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }""");`. It works but I just want to know if there's another solution. Also `GZipStream` may work. –  Aug 21 '16 at 00:05

2 Answers2

3

Shell32 is a COM server that you can use in your C# program. You however have to generate an interop assembly first to convert the type library inside shell32.dll (same idea as .NET metadata) to declarations that the CLR can understand. Either by running Tlbimp.exe or, much simpler, by adding a reference to the DLL in the IDE.

As long as you do this from the command line and don't use msbuild to get a .csproj project file compiled then you have to do the same thing that msbuild does, run tlbimp. For shell32.dll this only has to be done once and you can check-in the interop library in source control so you don't have to do it again. Use /r on the interop library.

Using the IDE or MSBuild.exe are of course the wise choices. Also helps you fall in the pit of success, you really want to use the Embed Interop Types feature so you don't need the interop assembly at runtime and don't have to deploy it. Looking at the build commands generated by MSBuild is useful.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Possibly because you are trying to reference it like a .NET assembly.

I would try to include the assembly using the /lib: argument instead as per...

/lib:dir1[,dir2] Compiler option.

War
  • 8,539
  • 4
  • 46
  • 98