none
download each file in folder RRS feed

Answers

  • Hi  Ahron321,

    Thanks for the feedback.

    If you want to download the file from ftp by using username and password, you could try the following code and the bold code is modified.

     private void Button1_Click(object sender, EventArgs e)
            {
    
    
                string uri = "ftp://172.17.13.127/";
                string username = "username";
                string password = "pwd";
                var list = FtplistFile(uri,username,password);
                string path = @"D:\Example";
                foreach (var item in list)
                {
                    Ftpdownloadfile(Path.Combine(uri, item), Path.Combine(path,item),username,password);
                }
    
                MessageBox.Show("success");
            }
            private List<string> FtplistFile(string url,string username,string password) //get file name form ftp folder
            {
                FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
                listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                listRequest.Credentials = new NetworkCredential(username, password);
                List<string> lines = new List<string>();
    
                using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
                using (Stream listStream = listResponse.GetResponseStream())
                using (StreamReader listReader = new StreamReader(listStream))
                {
                    while (!listReader.EndOfStream)
                    {
                        lines.Add(listReader.ReadLine());
                    }
                }
                List<string> name = new List<string>();
                foreach (var line in lines)
                {
                    string[] tokens =
                line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                    string n = tokens[3];
                    name.Add(n);
                }
                return name;
            }
    
            private void Ftpdownloadfile(string to_uri,string path,string username,string password)
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
    
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Create))
                        {
                            byte[] buffer = new byte[102400];
                            int read = 0;
                            do
                            {
                                read = responseStream.Read(buffer, 0, buffer.Length);
                                fs.Write(buffer, 0, read);
                                fs.Flush();
                            } while (!(read == 0));
    
                            fs.Flush();
                            fs.Close();
                        }
                    }
                }
    
    
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 17, 2019 1:14 AM
    Moderator

All replies

  • You can use the FtpWebRequest class. The documentation contains an example:

    https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest?view=netframework-4.8

    Wednesday, June 12, 2019 12:13 PM
    Moderator
  • it wont help
    Wednesday, June 12, 2019 12:39 PM
  • Please be far more specific. Since you evidently have existing code that doesn't work then please post it. Also post whatever error(s) you're getting including the stacktrace and line it is coming from. Also include any information about the FTP server that is relevant such as it requires authentication, passive connections, etc.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 12, 2019 1:35 PM
    Moderator
  • very simple, download all files in a folder thats in ftp host to some path
    Wednesday, June 12, 2019 1:49 PM
  • You said it won't help. Does that mean you wrote code and it doesn't work? Post the code. If you just need a code example there is one in MSDN on how to get the contents of a folder and then how to download each file. What part of the sample isn't working for you?

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 12, 2019 2:00 PM
    Moderator
  • Did you even LOOK at the link Alberto provided?  There is no magic "download all files" function.  You will have to do this exactly as you would as a human.  You will have to do one FtpWebRequest to list all the files, using the proper FTP command.  Then, you will have to do a series of FtpWebRequests, one for each file, to download them.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, June 12, 2019 8:35 PM
  • Hi Ahron321,

    Thank you for posting here.

    Based on your description, you want to download all files in a folder on ftp with c# code.

    You could try the following code.

     private void Button1_Click(object sender, EventArgs e)
            {
                string uri = "ftp://xxxx//";
                var list = FtplistFile(uri);
                string path = @"D:\Example";
                foreach (var item in list)
                {
                    Ftpdownloadfile(Path.Combine(uri, item), Path.Combine(path,item));
                }
    
                MessageBox.Show("success");
            }
            private List<string> FtplistFile(string url) //get file name form ftp folder
            {
                FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
                listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                List<string> lines = new List<string>();
    
                using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
                using (Stream listStream = listResponse.GetResponseStream())
                using (StreamReader listReader = new StreamReader(listStream))
                {
                    while (!listReader.EndOfStream)
                    {
                        lines.Add(listReader.ReadLine());
                    }
                }
                List<string> name = new List<string>();
                foreach (var line in lines)
                {
                    string[] tokens =
                line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                    string n = tokens[3];
                    name.Add(n);
                }
                return name;
            }
    
            private void Ftpdownloadfile(string to_uri,string path)
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Create))
                        {
                            byte[] buffer = new byte[102400];
                            int read = 0;
                            do
                            {
                                read = responseStream.Read(buffer, 0, buffer.Length);
                                fs.Write(buffer, 0, read);
                                fs.Flush();
                            } while (!(read == 0));
    
                            fs.Flush();
                            fs.Close();
                        }
                    }
                }
    
    
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 13, 2019 2:25 AM
    Moderator
  • where is the username and password string?
    Thursday, June 13, 2019 7:14 PM
  • Come on.  You need to take SOME responsibility for your own coding.  Your question would be trivially answered by 30 seconds with the documentation.  This is a peer help service, it is not a "please write and debug the code for me" service.  Hint:  The FtpWebRequest object has a Credentials property.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, June 14, 2019 6:17 PM
  • Hi  Ahron321,

    Thanks for the feedback.

    If you want to download the file from ftp by using username and password, you could try the following code and the bold code is modified.

     private void Button1_Click(object sender, EventArgs e)
            {
    
    
                string uri = "ftp://172.17.13.127/";
                string username = "username";
                string password = "pwd";
                var list = FtplistFile(uri,username,password);
                string path = @"D:\Example";
                foreach (var item in list)
                {
                    Ftpdownloadfile(Path.Combine(uri, item), Path.Combine(path,item),username,password);
                }
    
                MessageBox.Show("success");
            }
            private List<string> FtplistFile(string url,string username,string password) //get file name form ftp folder
            {
                FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
                listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                listRequest.Credentials = new NetworkCredential(username, password);
                List<string> lines = new List<string>();
    
                using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
                using (Stream listStream = listResponse.GetResponseStream())
                using (StreamReader listReader = new StreamReader(listStream))
                {
                    while (!listReader.EndOfStream)
                    {
                        lines.Add(listReader.ReadLine());
                    }
                }
                List<string> name = new List<string>();
                foreach (var line in lines)
                {
                    string[] tokens =
                line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                    string n = tokens[3];
                    name.Add(n);
                }
                return name;
            }
    
            private void Ftpdownloadfile(string to_uri,string path,string username,string password)
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
    
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Create))
                        {
                            byte[] buffer = new byte[102400];
                            int read = 0;
                            do
                            {
                                read = responseStream.Read(buffer, 0, buffer.Length);
                                fs.Write(buffer, 0, read);
                                fs.Flush();
                            } while (!(read == 0));
    
                            fs.Flush();
                            fs.Close();
                        }
                    }
                }
    
    
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 17, 2019 1:14 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 20, 2019 2:55 AM
    Moderator
  • You will need to connect to the server and authenticate here from the MSDOCS 

    public static bool DisplayFileFromServer(Uri serverUri) { // The serverUri parameter should start with the ftp:// scheme. if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } // Get the object used to communicate with the server. WebClient request = new WebClient(); // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); try { byte [] newFileData = request.DownloadData (serverUri.ToString()); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); Console.WriteLine(fileString); } catch (WebException e) { Console.WriteLine(e.ToString()); } return true; }

    //This boolean will tell you if the file was deleted based on the integer status received.

    public static bool DeleteFileOnServer(Uri serverUri) { // The serverUri parameter should use the ftp:// scheme. // It contains the name of the server file that is to be deleted. // Example: ftp://contoso.com/someFile.txt. // if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); request.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse) request.GetResponse(); Console.WriteLine("Delete status: {0}",response.StatusDescription); response.Close(); return true; }

    Thursday, June 20, 2019 3:37 AM