Hello,
The remote server returned an error: (550) File unavailable (e.g., file not found, no access) in c# .net windows application during .txt file download
When i using download file from ftp so its getting error:-
My zip file successfully downloading but my .txt file not downloading.
Please help me.
Two file in a folder on server ftp:-
TextureData its a folder name:-
ABC.zip,abc.txt two files in TextureData Folder.
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
What I have tried:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] files = ReadFileList();
FTPSettings.IP = "xx.xx.xxx.xxx/TextureData";
FTPSettings.UserID = "xxxx";
FTPSettings.Password = "xxx";
foreach (string file in files)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Proxy = null;
long fileSize;
using (WebResponse resp = request.GetResponse())
fileSize = resp.ContentLength;
request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = responseFileDownload.GetResponseStream())
using (FileStream writeStream = new FileStream(@"C:\Program Files (x86)\xxxx\" + file, FileMode.Create))
{
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
int bytes = 0;
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
bytes += bytesRead;
int iProgress = 0;
int totalSize = (int)(fileSize) / 1000;
if (totalSize > 0)
{
iProgress = (bytes / 1000) * 100 / totalSize;
}
backgroundWorker1.ReportProgress(iProgress, totalSize);
}
}
string zipFileExtension = Path.GetExtension(file);
if (zipFileExtension == ".zip")
{
string zipToUnpack = @"C:\Program Files (x86)\xxxx\" + file;
string unpackDirectory = @"C:\Program Files (x86)\xxxx\";
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
foreach (ZipEntry ze in zip1)
{
ze.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
}
}
public static class FTPSettings
{
public static string IP { get; set; }
public static string UserID { get; set; }
public static string Password { get; set; }
}
public string[] ReadFileList()
{
string[] mydownloadFiles;
StringBuilder myresult = new StringBuilder();
StreamReader myreader = null;
FtpWebRequest myreqFTP = null;
try
{
FTPSettings.IP = "xx.xx.xxx.xxx/TextureData";
FTPSettings.UserID = "xxxx";
FTPSettings.Password = "xxxx";
myreqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/"));
myreqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
myreqFTP.UseBinary = true;
myreqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
FtpWebResponse response = (FtpWebResponse)myreqFTP.GetResponse();
myreader = new StreamReader(response.GetResponseStream());
string myline = myreader.ReadLine();
while (myline != null)
{
myresult.Append(myline);
myresult.Append("\n");
myline = myreader.ReadLine();
}
myresult.Remove(myresult.ToString().LastIndexOf('\n'), 1);
return myresult.ToString().Split('\n');
}
catch (Exception ex)
{
if (myreader != null)
{
myreader.Close();
}
mydownloadFiles = null;
return mydownloadFiles;
}
}