System.UnauthorizedAccessException:拒绝访问路径:PATH

时间:2019-04-14 14:56:41

标签: c# webclient

出于某些原因,windows拒绝我的Web客户端下载。

它有权执行所有操作,但仍被拒绝。为什么?

代码:

web.DownloadFile("super epic link", location);

错误:

System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path 'C:\Users\JANGAJ GAMES\source\repos\html executer\html updater\bin\Debug' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at 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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at html_updater.Form1.Timer1_Tick(Object sender, EventArgs e) in C:\Users\JANGAJ GAMES\source\repos\html executer\html updater\Form1.cs:line 39
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1 个答案:

答案 0 :(得分:0)

这是因为IIS无法访问该文件。而是使用Path.GetDirectoryName(Application.ExecutablePath)访问应用程序的当前文件夹

var fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "download.txt");
web.DownloadFile("super epic link", fileName);