由于某种原因,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:UsersJANGAJ GAMESsourcereposhtml executerhtml updaterbinDebug' 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:UsersJANGAJ GAMESsourcereposhtml executerhtml updaterForm1.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)
这是因为IIS无法访问该文件。请改用Path.GetDirectoryName(Application.ExecutablePath)
访问应用的当前文件夹
var fileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "download.txt");
web.DownloadFile("super epic link", fileName);