Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtractToFile throwing Access Denied Error?

Tags:

c#

security

iis

First of all, I clear that I am using ASP.NET 4.5 with DefaultAppPool(integrated 4.0). I have also configured Anonymous Access to DefaultAppPool user. I have given all access to DefaultAppPool. System.Security.Principal.WindowsIdentity.GetCurrent() method give me the same user. But when the following line runs it gives me Access to the path 'XXXX' is denied. exception. Then for testing purpose, I have given all rights to EveryOne user but still getting the same error. Here is the line of code. Note the trus

        using (ZipArchive archive = new ZipArchive(zipStream))
        {
            foreach (ZipArchiveEntry file in archive.Entries)
            {
                file.ExtractToFile(location,true);
            }
        }

Here is the StackTrace,

[UnauthorizedAccessException: Access to the path 'XXX' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10793558
   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) +1352
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +65
   System.IO.Compression.ZipFileExtensions.ExtractToFile(ZipArchiveEntry source, String destinationFileName, Boolean overwrite) +96
   XX.XX.XX.ExtractZipFile(Stream zipStream, String location) in XX.cs:44
   XX.XX.XX.XX.XX.XX.ExtractZipFile(Stream zipStream, String location) in XXX.cs:17
like image 942
Imran Qadir Baksh - Baloch Avatar asked Feb 10 '13 06:02

Imran Qadir Baksh - Baloch


1 Answers

.NET framework in this case giving me the wrong exception. The problem was that, I was just passing location(folder location without file name). So after including complete path, this works.

like image 197
Imran Qadir Baksh - Baloch Avatar answered Sep 19 '22 15:09

Imran Qadir Baksh - Baloch