現在、次のコードを使用して、WebページからPDFファイルをダウンロードし、ユーザーが選択したフォルダに保存しようとしています。実行されるたびに、パスへのアクセスが拒否されたというメッセージが表示されます。WebClientリクエスト中のSystem.UnauthorisedAccessException C#
エラーメッセージ:
[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);
は、すべてのフォーラムを見ている試みたが、何のソリューションが動作するようには思えません。可能な限り解決策ではなく、何がうまくいかないかを知りたいのですが、私はコード作成を学びたいので、私はプロではありません。
おかげ
「みんな」のユーザーを追加し、あなたがする必要があるように見えますPDFをダウンロードする前に認証済み –
'destinationDirectory + title'の値は何ですか? –
値はC:\ ... Desktop \ TestFolder \ BlahBlah.pdf –