VB Error:
A first chance exception of type ‘System.UnauthorizedAccessException’ occurred in WindowsBase.dll
Detailed Error:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.UnauthorizedAccessException: Access to the path ‘C:\’ is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync)
at MS.Internal.IO.Zip.ZipArchive.OpenOnFile… path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(Str… path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess)
at ZipTutorial.Form1.ZipFiles() in C:\Users\James\documents\visual studio 2010\Projects\ZipTutorial\ZipTutorial\Fo… 10
at ZipTutorial.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\James\documents\visual studio 2010\Projects\ZipTutorial\ZipTutorial\Fo… 52
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventAr… e)
at System.Windows.Forms.Form.OnCreateContro…
at System.Windows.Forms.Control.CreateContr… fIgnoreVisible)
at System.Windows.Forms.Control.CreateContr…
at System.Windows.Forms.Control.WmShowWindo… m)
at System.Windows.Forms.Control.WndProc(Mes… m)
at System.Windows.Forms.ScrollableControl.W… m)
at System.Windows.Forms.Form.WmShowWindow(M… m)
at System.Windows.Forms.Form.WndProc(Messag… m)
at System.Windows.Forms.Control.ControlNati… m)
at System.Windows.Forms.Control.ControlNati… m)
at System.Windows.Forms.NativeWindow.Callba… hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
——————END ERROR
Full Code:
Imports System.IO.Packaging
Imports System.IO
Public Class Form1
Private Sub ZipFiles()
Dim zipPath As String
FolderBrowserDialog1.ShowDialog()
zipPath = “C:\”
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
‘Add as many files as you like:
AddToArchive(zip, “C:\Users\James\Pictures\zip.png”)
zip.Close() ‘Close the zip file
End Sub
Private Sub AddToArchive(ByVal zip As Package, _
ByVal fileToAdd As String)
‘Replace spaces with an underscore (_)
Dim uriFileName As String = fileToAdd.Replace(” “, “_”)
‘A Uri always starts with a forward slash “/”
Dim zipUri As String = String.Concat(“/”, _
IO.Path.GetFileName(uriFileName))
Dim partUri As New Uri(zipUri, UriKind.Relative)
Dim contentType As String = _
Net.Mime.MediaTypeNames.Application.Zip
‘The PackagePart contains the information:
‘ Where to extract the file when it’s extracted (partUri)
‘ The type of content stream (MIME type): (contentType)
‘ The type of compression: (CompressionOption.Normal)
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
contentType, CompressionOption.Normal)
‘Read all of the bytes from the file to add to the zip file
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
‘Compress and write the bytes to the zip file
pkgPart.GetStream().Write(bites, 0, bites.Length)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(“Click `OK` to select path to create .Zip”, “Select Path”)
ZipFiles()
End Sub
End Class
I get this Error no matter which directory I pick, whether it be the C drive or desktop.

As with most reported errors and exceptions, the first line gives you your first clue:
System.UnauthorizedAccessException: Access to the path ‘C:’ is denied.
Note the path mentioned: C:
That’s the root of C: I think from Windows Vista onward (possibly XP), the root was protected from casual access by users, so this could be a legitimate access denial. Try some directory you know you have access to, and see if you can get there.
If the reported directory isn’t the one you’re trying to get to, then you’re mangling it somehow and that needs to be fixed.
Hope that helps.
I had this problem too when I started to prgram with windows 7. You need to save your file to appdata folder. Within this folder, create another folder for each of your programs you create. This will keep everything neat and tidy.
Environment.GetFolderPath(Environment.…