0

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

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).

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. I am primarily concerned with the UnauthorizedAccessException and how to fix that.

avidprogrammer
  • 57
  • 1
  • 11
  • do you have access to the FTP folder? If you supply credentials to the ftp are they correct. – Mo Patel Apr 21 '13 at 20:48
  • Yes. I can add request.Credentials = new NetworkCredentails("u","p"); and I will still get the UnAuthEx. – avidprogrammer Apr 21 '13 at 20:53
  • Verify that the user account has read permissions for that path and file. – Tim Apr 21 '13 at 21:03
  • Make sure the final path you use is actually path to a file... From code sample it feels like you may be trying to create file with the same name as folder. – Alexei Levenkov Apr 21 '13 at 21:04
  • Your local file has nothing to do with FTP at all. You can just delete everything FTP related from this question. It is a red herring that already has confused you and others. – usr Apr 21 '13 at 21:08

1 Answers1

0

If you are on Win10 this line may cause errors:

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 (e.g. c:\TMP).