Reputation: 313
I'm trying to backup some files using the .NET 4.5 ZipFile class and the CreateFromDirectory(string, string) method. I'm getting an UnauthorizedAccessException - Access Denied. I can successfully read all files in that directory as well as write a file to that directory. So I would think that the permissions are set up properly. Any thoughts on why I'm getting access denied on the ZipFile class?
static void Main(string[] args)
{
string backupLocation = @"C:\Backups";
string directoriesToBackup = @"F:\myMedia\myPictures\Our Family\2012\Misc";
try
{
ZipFile.CreateFromDirectory(directoriesToBackup, backupLocation);
}
catch (System.UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
DirectoryInfo di = new DirectoryInfo(@"F:\myMedia\myPictures\Our Family\2012\Misc");
File.Create(@"F:\myMedia\myPictures\Our Family\2012\Misc\testCreateFromVs.txt");
foreach (FileInfo i in di.GetFiles())
{
Console.WriteLine(i.Name);
}
Console.ReadKey();
}
Upvotes: 14
Views: 15633
Reputation: 1042
In my case, I was working with Unity and I had manually added System.IO.Compression.dll and System.IO.Compression.ZipFile.dll (and relevant XMLs) into Assets folder.
But I chose a DLL with wrong SDK version, and while it worked fine on MacOs, it gave UnauthorizedAccessException in Android and iOS.
I solved the issue by removing dll's and XML's and following the thread on Unity Forum .
Basically you have to make sure your project Api Compatibility Level and dll .Net version matches.
Upvotes: 0
Reputation: 610
The problem can also arises when a folder with the same name as the (output) zip already exists
Upvotes: 3
Reputation: 554
In my case I was trying to create the target directory before I started to zip the file there, but was creating the target directory as the name of the zip file, so because the empty zip file already existed (as a directory), I got the same error.
Upvotes: 2
Reputation: 11968
It seems you have misunderstood something.
backupLocation = @"C:\Backups";
you want to overwrite the directory "C:\Backups" with a file ! That's not allowed! ;-) (Access Denied)
You have to specify the path with file name.
Syntax: CreateFromDirectory(string,string)
public static void CreateFromDirectory(
string sourceDirectoryName,
string destinationArchiveFileName
)
Example:
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
[...]
Upvotes: 26