Reputation:
I'm trying to download a file through my application but I get an UnauthorizedAccessException
yet the link is valid. What is wrong?
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("https://drive.google.com/uc?export=download&id=1dyuz10HTPIpJtXtIc7mDc2eHLJspo3lp", @"c:\ikon.png");
}
'NTI-X.exe' (CLR v4.0.30319: NTI-X.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_sv_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols. Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll 'NTI-X.exe' (CLR v4.0.30319: NTI-X.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.resources\v4.0_4.0.0.0_sv_b77a5c561934e089\System.resources.dll'. Module was built without symbols. Exception thrown: 'System.Net.WebException' in System.dll 'NTI-X.exe' (CLR v4.0.30319: NTI-X.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Transactions.resources\v4.0_4.0.0.0_sv_b77a5c561934e089\System.Transactions.resources.dll'. Module was built without symbols. System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledOhanterat undantagNTI-X.exeSystem.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Ett undantag uppstod under en WebClient-begäran. vid System.Net.WebClient.DownloadFile(Uri address, String fileName) vid System.Net.WebClient.DownloadFile(String address, String fileName) vid NTI_X.Main..ctor() i C:\Users\elev\Documents\Visual Studio 2017\Projects\NTI-X\NTI-X\Main.cs:rad 108 vid NTI_X.Program.Main() i C:\Users\elev\Documents\Visual Studio 2017\Projects\NTI-X\NTI-X\Program.cs:rad 19System.Net.WebException: Ett undantag uppstod under en WebClient-begäran. ---> System.UnauthorizedAccessException: Åtkomst till sökvägen c:\ikon.png nekas. vid System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) vid 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) vid System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) vid System.Net.WebClient.DownloadFile(Uri address, String fileName) --- Slut på stackspårning för interna undantag --- vid System.Net.WebClient.DownloadFile(Uri address, String fileName) vid System.Net.WebClient.DownloadFile(String address, String fileName) vid NTI_X.Main..ctor() i C:\Users\elev\Documents\Visual Studio 2017\Projects\NTI-X\NTI-X\Main.cs:rad 108 vid NTI_X.Program.Main() i C:\Users\elev\Documents\Visual Studio 2017\Projects\NTI-X\NTI-X\Program.cs:rad 19System.UnauthorizedAccessException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Åtkomst till sökvägen c:\ikon.png nekas. vid System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) vid 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) vid System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) vid System.Net.WebClient.DownloadFile(Uri address, String fileName)System.UnauthorizedAccessException: Åtkomst till sökvägen c:\ikon.png nekas. vid System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) vid 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) vid System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) vid System.Net.WebClient.DownloadFile(Uri address, String fileName) An unhandled exception of type 'System.Net.WebException' occurred in System.dll Ett undantag uppstod under en WebClient-begäran.
Upvotes: 1
Views: 495
Reputation: 1062502
We can see a WebException
that is surfacing as an UnauthorizedAccessException
; sounds like either a 401 on the url ("https://drive.google.com/uc?export=download&id=1dyuz10HTPIpJtXtIc7mDc2eHLJspo3lp"
), or that you don't have write access to the local download path (@"c:\ikon.png"
) - my money would be on the second one: most operating systems don't like you writing to the file system root.
To try to identify which:
curl
or similar, or an anonymous browser session, can download the urlTesting the link locally: yes, the url seems fine - reinforcing that that chosen file location is the most likely problem.
Upvotes: 3