C# webClient 下载确实抛出错误
C# webClient download does throw error
我正在尝试通过我的应用程序下载文件,但我收到 UnauthorizedAccessException
,但 link 有效。怎么了?
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.
我们可以看到 WebException
作为 UnauthorizedAccessException
浮出水面;听起来像是 url ("https://drive.google.com/uc?export=download&id=1dyuz10HTPIpJtXtIc7mDc2eHLJspo3lp"
) 上的 401,或者您没有对本地下载路径的写入权限 (@"c:\ikon.png"
) - 我的钱会花在第二个上: 大多数操作系统不喜欢您写入文件系统根目录。
尝试确定哪个:
- 查看是否
curl
或类似的,或匿名浏览器会话,可以下载url
- 尝试使用不同的文件位置
在本地测试 link:是的,url 看起来不错 - 强调所选的文件位置是最有可能的问题。
我正在尝试通过我的应用程序下载文件,但我收到 UnauthorizedAccessException
,但 link 有效。怎么了?
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.
我们可以看到 WebException
作为 UnauthorizedAccessException
浮出水面;听起来像是 url ("https://drive.google.com/uc?export=download&id=1dyuz10HTPIpJtXtIc7mDc2eHLJspo3lp"
) 上的 401,或者您没有对本地下载路径的写入权限 (@"c:\ikon.png"
) - 我的钱会花在第二个上: 大多数操作系统不喜欢您写入文件系统根目录。
尝试确定哪个:
- 查看是否
curl
或类似的,或匿名浏览器会话,可以下载url - 尝试使用不同的文件位置
在本地测试 link:是的,url 看起来不错 - 强调所选的文件位置是最有可能的问题。