简体   繁体   English

UnauthorizedAccessException

[英]UnauthorizedAccessException

I am working with the tutorial here: http://www.devtoolshed.com/content/c-download-file-progress-bar and I am receiving an UnauthorizedAccessException on the following line of code 我正在使用此教程: http : //www.devtoolshed.com/content/c-download-file-progress-bar,并且在以下代码行中收到UnauthorizedAccessException

using (Stream streamLocal = new FileStream(sPathToWriteFilesTo, 
FileMode.Create, FileAccess.Write, FileShare.None)) 
{ 
    //... 
}

Perhaps it is something with permissions of the file I am trying to read? 也许这是我尝试读取的文件的权限? It is just a simple text file requested using FTP (instead of HTTP as the tutorial shows). 它只是使用FTP请求的简单文本文件(而不是本教程显示的HTTP)。

string sFtpToReadFilesFrom = "ftp://<user>:<pass>@mysite.tk/updates/App_Data/output_log.txt";
string sPathToWriteFilesTo = Application.StartupPath + "\\App_Data";

Uri url = new Uri(sFtpToReadFilesFrom);
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);
request.Timeout = 10000;
request.ReadWriteTimeout = 10000;
request.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();

If anyone sees any immediate red flags with the FtpWebRequest please do let me know. 如果有人看到FtpWebRequest有任何立即的危险信号,请告诉我。 I am primarily concerned with the UnauthorizedAccessException and how to fix that. 我主要关心UnauthorizedAccessException及其解决方法。

If you are on Win10 this line may cause errors: 如果您使用的是Win10,则此行可能会导致错误:

string sPathToWriteFilesTo = Application.StartupPath + "\\App_Data";

Since one of updates you can't copy files from network location directly into some folders. 由于是更新之一,因此无法将文件从网络位置直接复制到某些文件夹中。 Try downloads folder or some directly at C drive (eg c:\\TMP ). 尝试直接在C驱动器上下载文件夹或某些文件夹(例如c:\\TMP )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM