WebClient请求C#期间System.UnauthorisedAccessException

时间:2017-08-22 11:30:11

标签: c# asp.net .net

目前使用以下代码尝试从网页下载PDF文件并将其存储在用户选择的文件夹中。每次运行时我都会收到一条消息,告诉我拒绝访问路径。

错误讯息:

  [0] "Link was not downloaded due to: System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path ***** is denied.\r\n   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n   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)\r\n   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)\r\n   at System.Net.WebClient.DownloadFile(Uri address, String fileName)\r\n   --- End of inner exception stack trace ---\r\n   at System.Net.WebClient.DownloadFile(Uri address, String fileName)\r\n   at System.Net.WebClient.DownloadFile(String address, String fileName)\r\n   at TextFileToDownloadedPDF.Form1.DownloadPDF() in *****\\Form1.cs:line 103 link address: *address to download pdf, removed for security reasons* object {string}

代码:

string source = client.DownloadString(htmlLink);
string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

client.DownloadFile(htmlLink, destinationDirectory + title);

试图查看所有论坛,但似乎没有解决方案。我想知道出了什么问题不仅仅是一个解决方案,如果可能的话,因为我正在努力学习编码,我不是专业人士。

由于

1 个答案:

答案 0 :(得分:0)

右键单击该文件夹,转到权限并添加&#34;网络服务&#34; user - 右键单击​​该文件夹,转到权限并添加&#34; Everyone&#34;用户