11 Replies - 378 Views - Last Post: 31 January 2018 - 11:54 PM Rate Topic: -----

#1 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

'CompressionLevel' is ambiguous

Posted 30 January 2018 - 06:32 AM

Hi there

I am trying to compress files using the following code:

Imports System.IO
Imports System.IO.Compression
Module Module1
    Sub Main()
        Dim SourceDirName As String = "DirName"
        Dim DestinationArchiveName As String = "ArchiveName"
        ZipFile.CreateFromDirectory(SourceDirName, DestinationArchiveName, CompressionLevel.Optimal, False)
    End Sub
End Module



[SourceDirName and DestinationArchiveName are assigned those values just for this post, but I have the directory names and archive names assigned properly in my code.]

I just started coding it and there is already an error. For some reason, I keep getting this error: 'CompressionLevel' is ambiguous in the namespace 'System.IO.Compression'

Steps I have tried so far:
  • I have used the Imports System.IO and System.IO.Compression as shown above.
  • I've tried removing System.IO but it causes errors in my other code.
  • I've tried using Imports System.IO.Compression.FileSystem, but it does nothing. I do however, have System.IO.Compression.FileSystem in my list of references and in the assemblies folder. I've tried removing it to see if it's causing any conflicts, but it still throws an error and a new error by ZipFile arises.
  • I have researched this error, but can't find any help on this issue. One of the MS websites states the name must be fully qualified. So I tried using
    Compression.CompressionLevel.Optimal
    
    but even that throws the same error.
    System.IO.Compression.CompressionLevel.Optimal
    
    doesn't work either.


Can someone please help me pin point what the problem is? I've been sitting with this for 6 hours now and really need help.

This post has been edited by Slinky: 30 January 2018 - 06:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 'CompressionLevel' is ambiguous

#2 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,532
  • Joined: 07-September 13

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 06:43 AM

What is ZipFile? what assembly does that live in?

Never mind, found the.Net reference to it and realised I needed to add in references
Was This Post Helpful? 0
  • +
  • -

#3 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 06:45 AM

View Postmaceysoftware, on 30 January 2018 - 01:40 PM, said:

What is ZipFile? what assembly does that live in?


It lives in the assembly System.IO.Compression.FileSystem, it is currently in the assemblies folder, and the ZipFile part works, but the CompressionLevel keeps throwing an error. Tried using System.IO.Compression.FileSystem.CompressionLevel.Optimal throws another error: FileSystem is not a member of 'System.IO.Compression'
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,532
  • Joined: 07-September 13

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 06:45 AM

Strange that you're getting this error? I have just chucked the same code into a console app, added the reference System.Io.Compression.FileSystem and it is compiling for me, I mean it throws an error because i left the variables declared as they were.
Was This Post Helpful? 0
  • +
  • -

#5 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 06:58 AM

View Postmaceysoftware, on 30 January 2018 - 01:45 PM, said:

Strange that you're getting this error? I have just chucked the same code into a console app, added the reference System.Io.Compression.FileSystem and it is compiling for me, I mean it throws an error because i left the variables declared as they were.

Here's the screenshot of my code and the error

Sorry, I'm having trouble attaching a file, an error is thrown.

This post has been edited by Slinky: 30 January 2018 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

#6 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 121
  • Joined: 26-September 16

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 07:37 AM

The following code works for me:

Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim startPath As String = "E:\Test"
        Dim zipPath As String = "E:\result.zip"

        ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, True)
    End Sub

End Module


But it's pretty much the same as your code except for the compression level. It's essentially taken directly from the documentation. Maybe try "cleaning" your project because there appears to be no reasonable explanation. You can also try removing all of the dependencies and including only the ones you need. Or perhaps your installation of .NET Framework is broken. There could be many reasons, I suppose.
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: 'CompressionLevel' is ambiguous

Posted 30 January 2018 - 09:10 AM

This error is caused from you having 2 namespaces exposed in your application's code, and both namespaces are using the same name for something within each of them, in your case it would be the name 'CompressionLevel' that is being used in both namespaces. The compiler is telling you that 'CompressionLevel' could be two different things and it does not know which to use.

Since none of the three of us seem to get the 'Ambiguous' error with your example code, it would suggest you have added a reference in your project to a namespace which we are not aware of.

One way to get around the problem is to get rid of the Import statements and use the full namespace for using the System.IO.Compression namespace. For example...
'Imports System.IO.Compression 'commented out, not used

Module Module1
    Sub Main()
        Dim SourceDirName As String = "DirName"
        Dim DestinationArchiveName As String = "ArchiveName"
        System.IO.Compression.ZipFile.CreateFromDirectory(SourceDirName, DestinationArchiveName, System.IO.Compression.CompressionLevel.Optimal, False)
    End Sub
End Module



Depending on how much you use this namespace in your code, it could get crazy adding 'System.IO.Compression.' to the beginning of all the code. Instead, you can assign a specific (short) name for the full namespace which you can use throughout your code. For example, below i use Comp as the name for the 'System.IO.Compression' namespace. This should also stop the 'Ambiguous' error too.
Imports Comp = System.IO.Compression

Module Module1
    Sub Main()
        Dim SourceDirName As String = "DirName"
        Dim DestinationArchiveName As String = "ArchiveName"
        Comp.ZipFile.CreateFromDirectory(SourceDirName, DestinationArchiveName, Comp.CompressionLevel.Optimal, False)
    End Sub
End Module


Was This Post Helpful? 0
  • +
  • -

#8 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

Re: 'CompressionLevel' is ambiguous

Posted 31 January 2018 - 04:15 AM

Hi there. I've tried doing it both ways and it still did not work. I created a new file and inserted that code alone so there's no other code interfering with it and it is still giving me the error. I can't seem to upload images here to show you my code and the errors it's displaying.

I've had this error before with other code, and fully qualifying the name did help, but for some reason, it's not working here. System.IO.Compression.CompressionLevel is the fully qualified name, which is why I can't understand why it is doing this. I can't seem to find what is causing this conflict.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,532
  • Joined: 12-December 12

Re: 'CompressionLevel' is ambiguous

Posted 31 January 2018 - 04:20 AM

Quote

I can't seem to upload images here to show you my code and the errors it's displaying.

Posting images directly to the site is broken unfortunately. You can use an external service such as imgur.

However, for code and error messages, paste them directly here in the thread between code or quote tags. Code and error messages should be visible here directly with the topic.
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: 'CompressionLevel' is ambiguous

Posted 31 January 2018 - 12:45 PM

As andrewsw has said, post your code here using the [CODE] button to place it between code tags. Also, tell us exactly what assemblies you have added a reference to. For example, i only added 1 reference to 'System.IO.Compression.FileSystem' in the Reference Manager for my tests and examples.

It would seem to me that there must be something in the current namespaces which is using the same name as 'CompressionLevel', or you would not be getting the 'Ambiguous' error you originally posted. Although, I guess there could be the chance of a corrupted installation of Visual Studio but, i would be more apt to think it is not.

Also, you are creating a VB.Net Console Application, right?
Was This Post Helpful? 0
  • +
  • -

#11 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

Re: 'CompressionLevel' is ambiguous

Posted 31 January 2018 - 11:42 PM

Okay, basically, this is my entire code including references:
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim SourceDirName As String = "DirectoryName"
        Dim DestinationArchiveName As String = "ArchiveName"
        ZipFile.CreateFromDirectory(SourceDirName, DestinationArchiveName, Compression.CompressionLevel.Optimal, False)
    End Sub

End Module



  • System.IO.Compression.FilSystem does not pick up in the code, but it is included in the list of references in the project folder.
  • Removing Compression from Comrpession.CompressionLevel still causes the same error.
  • Removing System.IO still causes the same error.


I've tried all the above with no luck

This post has been edited by Slinky: 31 January 2018 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#12 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 14-April 14

Re: 'CompressionLevel' is ambiguous

Posted 31 January 2018 - 11:54 PM

I found the problem! I re-checked the references in the project folder and found a System.Shim reference which I did not recognise, so I removed it and that solved the problem!! :bananaman:
Thanks guys :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1