Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

C#
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:

C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
string[] files = ReadFileList();
                    FTPSettings.IP = "xx.xx.xxx.xxx/TextureData";
                    FTPSettings.UserID = "xxxx";
                    FTPSettings.Password = "xxx";
                    //FtpWebRequest reqFTP = null;
                    //Stream ftpStream = null;
                    foreach (string file in files)
                    {
                        //string fileName = e.Argument.ToString();

                        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; // this is the key for ReportProgress
                        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;// don't forget to increment bytesRead !
                                int iProgress = 0;
                                int totalSize = (int)(fileSize) / 1000; // Kbytes
                                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()
        {
            //Debugger.Break();
            string[] mydownloadFiles;
            StringBuilder myresult = new StringBuilder();
            //WebResponse myresponse = null;
            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();
                }
                // to remove the trailing '\n'
                myresult.Remove(myresult.ToString().LastIndexOf('\n'), 1);
                return myresult.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                if (myreader != null)
                {
                    myreader.Close();
                }
                mydownloadFiles = null;
                return mydownloadFiles;
            }
        }
Posted
Updated 19-Oct-16 18:19pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900