Skip to main content
 首页 » 编程设计

C# webClient 下载确实抛出错误

2024年02月25日34yjmyzz

我正在尝试通过我的应用程序下载文件,但我收到了 UnauthorizedAccessException,但链接有效。怎么了?

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.

请您参考如下方法:

我们可以看到作为 UnauthorizedAccessException 出现的 WebException;听起来像是 URL 上的 401 ("https://drive.google.com/uc?export=download&id=1dyuz10HTPIpJtXtIc7mDc2eHLJspo3lp"),或者您没有本地下载的写入权限路径 (@"c:\ikon.png") - 我的钱会花在第二个上:大多数操作系统不喜欢你写入文件系统根目录。

尝试确定是哪一个:

  • 查看 curl 或类似的,或匿名浏览器 session 是否可以下载 url
  • 尝试使用不同的文件位置

在本地测试链接:是的,该 url 似乎没问题 - 强调所选的文件位置是最有可能的问题。