C#webClient下载确实会抛出错误

问题描述 投票:1回答:1

我正在尝试通过我的应用程序下载文件,但我得到一个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):已加载'C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ mscorlib.resources \ v4.0_4.0.0.0_sv_b77a5c561934e089 \ mscorlib.resources .dll文件”。模块没有符号。抛出异常:mscorlib.dll'NTI-X.exe'中的'System.UnauthorizedAccessException'(CLR v4.0.30319:NTI-X.exe):已加载'C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.resources \ v4.0_4.0.0.0_sv_b77a5c561934e089 \ System.resources.dll”。模块没有符号。抛出异常:System.dll'NTI-X.exe'中的'System.Net.WebException'(CLR v4.0.30319:NTI-X.exe):已加载'C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System .Transactions.resources \ v4.0_4.0.0.0_sv_b77a5c561934e089 \ System.Transactions.resources.dll”。模块没有符号。 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 uppst 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:在WebClient-begäran下的Ett undantag uppstod。 ---> System.UnauthorizedAccessException:Åtkomst直到sökvägenc:\ 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)--- Slutpåstackspårningförinternaundantag --- 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 = b77a5c561 934e089Åtkomst直到sökvägenc:\ 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直到sökvägenc:\ 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)未处理的异常在“WebClient-begäran”下的System.dll Ett undantag uppstod中出现“System.Net.WebException”类型。

c# winforms webclient-download
1个回答
3
投票

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

试图找出哪个:

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

在本地测试链接:是的,网址似乎很好 - 强化所选文件位置是最可能的问题。

© www.soinside.com 2019 - 2024. All rights reserved.