bezzad / Downloader

Fast, cross-platform and reliable multipart downloader with asynchronous progress events for .NET applications.
MIT License
1.25k stars 193 forks source link

Access to the path is denied. #117

Closed kyuranger closed 1 year ago

kyuranger commented 1 year ago

Hi, the nuget package I am using is 3.0.1 and the target framework is .net 7 of WPF.

Here is my code:

DownloadService Downloader;
var downloadOpt = new DownloadConfiguration()
{
                // usually, hosts support max to 8000 bytes, default values is 8000
                BufferBlockSize = 8000,
                // file parts to download, default value is 1
                ChunkCount = 8,
                // download speed limited to 2MB/s, default values is zero or unlimited
                MaximumBytesPerSecond = 1024 * 1024 * 2,
                // the maximum number of times to fail
                MaxTryAgainOnFailover = 5,
                // download parts of file as parallel or not. Default value is false
                ParallelDownload = true,
                // number of parallel downloads. The default value is the same as the chunk count
                ParallelCount = 4,
                // timeout (millisecond) per stream block reader, default values is 1000
                Timeout = 1000,
                // set true if you want to download just a specific range of bytes of a large file
                RangeDownload = false,
                // floor offset of download range of a large file
                RangeLow = 0,
                // ceiling offset of download range of a large file
                RangeHigh = 0,
                // clear package chunks data when download completed with failure, default value is false
                ClearPackageOnCompletionWithFailure = true,
                CheckDiskSizeBeforeDownload = true,
                ReserveStorageSpaceBeforeStartingDownload = true,
};
Downloader = new DownloadService(downloadOpt);
try
{                
Downloader.DownloadFileTaskAsync("https://dw11.uptodown.com/dwn/ssqNkZIxVNKMWHTLCmh2pYJTO12K3HDUaQD4NHAbLaRT-teJVu1w1WDFRKKith4-LwMTFwf7kVoltISnqld6dvah6_zBqnAAEBn5gx7mW7vufg91ZNiZzK6FkWB_23GE/YVwpUEj1_dLqJhSoSURvH-LxChYiwS1POc5feAMLW1Re4UuXFq8jBhNEg6-2m9ex7kvPdIqUwJMXpAzb5kakOwVvcOei77jtVF1d4VZnJa68L1o3aHtSrGavFELDBT-u/AtWSi9BdCBj8FWg2JmCrnTmRZWx1-9ei01iB_tGYEPkJT9_l8imxnwfd0DLbWuvmCyo5aGIrJdNXlOC8WWKipbgJ4CPxs7Y5j0Xk9PJoXLU=/windows-10-22h2-build-19045.iso", "C:\\Users\\mywat\\Downloads");
}
catch (Exception ex)
{
}

After the code ran, the output reports the error as below: Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Sockets.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Requests.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Net.Requests.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll

Soon, the try&catch report an error: Access to the path 'C:\Users\mywat\Downloads' is denied.

Even I change different path or different disk, the error still here.

What's wrong with it?

bezzad commented 1 year ago

Hi @kyuranger This problem depends on your user permission and access level when executing an application on OS the application can't achieve more access than the user.

Ensure that the folder structure under C:\Users\mywat\Downloads is not hidden and that the user attempting to save has full write permissions on the folders and files. Usually, an administrator on the machine can do this inside Windows Explorer by right-mouse clicking on the Downloads folder, selecting Properties > General and setting the Read-only and Hidden attributes to unchecked.

To ensure your app could be ok, do the below test to ensure your user account has permission on that folder.

string downloadFilePath = @"C:\Users\mywat\Downloads\file.txt";
FileStream fs = new FileStream(downloadFilePath, FileMode.Create);
kyuranger commented 1 year ago

I just did this with a try&catch. However, it creates the file successfully without any errors.

kyuranger commented 1 year ago

I tried to downgrade the package to 2.4.1. Now it won't report the Access denied error any more.

Whereas, it still reports the errors in output as below and cannot download the file any: Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Sockets.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Requests.dll Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Net.Requests.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll

kyuranger commented 1 year ago

I found out the problem.

My code should modify from this: Downloader.DownloadFileTaskAsync("https://dw11.uptodown.com/dwn/ssqNkZIxVNKMWHTLCmh2pYJTO12K3HDUaQD4NHAbLaRT-teJVu1w1WDFRKKith4-LwMTFwf7kVoltISnqld6dvah6_zBqnAAEBn5gx7mW7vufg91ZNiZzK6FkWB_23GE/YVwpUEj1_dLqJhSoSURvH-LxChYiwS1POc5feAMLW1Re4UuXFq8jBhNEg6-2m9ex7kvPdIqUwJMXpAzb5kakOwVvcOei77jtVF1d4VZnJa68L1o3aHtSrGavFELDBT-u/AtWSi9BdCBj8FWg2JmCrnTmRZWx1-9ei01iB_tGYEPkJT9_l8imxnwfd0DLbWuvmCyo5aGIrJdNXlOC8WWKipbgJ4CPxs7Y5j0Xk9PJoXLU=/windows-10-22h2-build-19045.iso", "C:\Users\mywat\Downloads");

To this: DirectoryInfo path = new DirectoryInfo("C:\Users\mywat\Downloads"); Downloader.DownloadFileTaskAsync("https://dw11.uptodown.com/dwn/ssqNkZIxVNKMWHTLCmh2pYJTO12K3HDUaQD4NHAbLaRT-teJVu1w1WDFRKKith4-LwMTFwf7kVoltISnqld6dvah6_zBqnAAEBn5gx7mW7vufg91ZNiZzK6FkWB_23GE/YVwpUEj1_dLqJhSoSURvH-LxChYiwS1POc5feAMLW1Re4UuXFq8jBhNEg6-2m9ex7kvPdIqUwJMXpAzb5kakOwVvcOei77jtVF1d4VZnJa68L1o3aHtSrGavFELDBT-u/AtWSi9BdCBj8FWg2JmCrnTmRZWx1-9ei01iB_tGYEPkJT9_l8imxnwfd0DLbWuvmCyo5aGIrJdNXlOC8WWKipbgJ4CPxs7Y5j0Xk9PJoXLU=/windows-10-22h2-build-19045.iso",path );

Now it works.

bezzad commented 1 year ago

I guessed you were set the path instead of the full name. When you set the second parameter as a string then you must give a full file name. If you want to give the directory name so create a DirectoryInfo.

kyuranger commented 1 year ago

I guessed you were set the path instead of the full name. When you set the second parameter as a string then you must give a full file name. If you want to give the directory name so create a DirectoryInfo.

Yes, that's exactly what I fault.