EasyE
EasyE

Reputation: 580

Access Denied Error when using ZipFile Class

Background

I currently have a usb device which has all my projects which I am working on and it stays in my office's docking station. Last night I was bored and wanted to continue work on a project, However no files? This made me think about writing a console app that would copy all the files from my usb drive to a folder on my desktop and would run as a scheduled task at a certain hour.

Problem

I am trying to use a System.IO.Compression.FileSystem in order to compress the file in order in order to then move it to a new directory on my PC. The documentation gives an example here ZipFile class. IT seems very up front. However, when I am trying to copy it to a new destination I get the following error.

Error

Message

Access to the path 'C:\Users\er4505\Desktop' is denied.

StackTrace

  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, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, Nullable`1 compressionLevel, Boolean includeBaseDirectory, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.CreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName)
   at ConsoleApp1.Program.Main(String[] args) in C:\FTG\Projects\CaseWare\ConsoleApp1\Program.cs:line 25

Code

 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
 string[] folders = System.IO.Directory.GetDirectories(@"D:\", "*", System.IO.SearchOption.TopDirectoryOnly);
 foreach (string folder in folders)
 {
     ZipFile.CreateFromDirectory(folder, path); << errors out here!
 }

Upvotes: 0

Views: 6242

Answers (1)

Ramius
Ramius

Reputation: 141

The 2nd parameter string destinationArchiveFileName should be a filename. for Example "C:\Users\er4505\Desktop\example.zip"

Upvotes: 2

Related Questions