locked
ftp folder RRS feed

  • Question

  • how to create a folder and check an existing folder on ftp server using asp.net
    Monday, May 4, 2009 12:03 PM

Answers

  • Hi Mukesh,

    I had a similar requirement in one of my projects and I used the following approach using C#:

    1. List all directories on FTP server.
    2. Check if the directory that you want to create, already exists.
    3. If it does not exist, create a directory. 

    Below is the code that you can use:

            private void MakeDir(string dirName)
            {
                FtpWebResponse response = null;
                Stream ftpStream = null;
                try
                {
                    FtpWebRequest reqFTP;
                    string[] Files = GetFileList();
                    ArrayList arrDirectories = new ArrayList();
                    if (Files != null)
                    {
                        foreach (string dir in Files)
                        {
                            arrDirectories.Add(dir);
                        }
                    }
                    if (!arrDirectories.Contains(dirName))
                    {
                        // dirName = name of the directory to create.
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
                        reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                        reqFTP.UseBinary = true;
                        reqFTP.KeepAlive = false;
                        reqFTP.Proxy = null;
                        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                        response = (FtpWebResponse)reqFTP.GetResponse();
                        ftpStream = response.GetResponseStream();                                        
                    }
                }
                catch (Exception ex)
                {
                    if (ftpStream != null)
                    {
                        ftpStream.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }               
                }
            }
    
            public string[] GetFileList()
            {
                string[] downloadFiles;
                StringBuilder result = new StringBuilder();
                WebResponse response = null;
                StreamReader reader = null;
                try
                {
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    reqFTP.Proxy = null;
                    reqFTP.KeepAlive = false;
                    reqFTP.UsePassive = false;
                    response = reqFTP.GetResponse();
                    reader = new StreamReader(response.GetResponseStream());
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    // to remove the trailing '\n'
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    return result.ToString().Split('\n');
                }
                catch (Exception ex)
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }                
                    downloadFiles = null;
                    return downloadFiles;
                }
            }
    Feel free to ask any queries regarding the same.

    Sumit Lathwal
    [Please do not forget to mark as answer for a correct reply]
    • Proposed as answer by sumitlathwal Tuesday, May 5, 2009 6:30 AM
    • Marked as answer by Mukesh Kumar Tuesday, May 5, 2009 6:33 AM
    Tuesday, May 5, 2009 6:30 AM

All replies

  • how to create a folder on ftp server and check whether a folder exists or not on ftp server using  (C#)

    Tuesday, May 5, 2009 4:35 AM
  • Hi Mukesh,

    I had a similar requirement in one of my projects and I used the following approach using C#:

    1. List all directories on FTP server.
    2. Check if the directory that you want to create, already exists.
    3. If it does not exist, create a directory. 

    Below is the code that you can use:

            private void MakeDir(string dirName)
            {
                FtpWebResponse response = null;
                Stream ftpStream = null;
                try
                {
                    FtpWebRequest reqFTP;
                    string[] Files = GetFileList();
                    ArrayList arrDirectories = new ArrayList();
                    if (Files != null)
                    {
                        foreach (string dir in Files)
                        {
                            arrDirectories.Add(dir);
                        }
                    }
                    if (!arrDirectories.Contains(dirName))
                    {
                        // dirName = name of the directory to create.
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
                        reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                        reqFTP.UseBinary = true;
                        reqFTP.KeepAlive = false;
                        reqFTP.Proxy = null;
                        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                        response = (FtpWebResponse)reqFTP.GetResponse();
                        ftpStream = response.GetResponseStream();                                        
                    }
                }
                catch (Exception ex)
                {
                    if (ftpStream != null)
                    {
                        ftpStream.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }               
                }
            }
    
            public string[] GetFileList()
            {
                string[] downloadFiles;
                StringBuilder result = new StringBuilder();
                WebResponse response = null;
                StreamReader reader = null;
                try
                {
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    reqFTP.Proxy = null;
                    reqFTP.KeepAlive = false;
                    reqFTP.UsePassive = false;
                    response = reqFTP.GetResponse();
                    reader = new StreamReader(response.GetResponseStream());
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    // to remove the trailing '\n'
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    return result.ToString().Split('\n');
                }
                catch (Exception ex)
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }                
                    downloadFiles = null;
                    return downloadFiles;
                }
            }
    Feel free to ask any queries regarding the same.

    Sumit Lathwal
    [Please do not forget to mark as answer for a correct reply]
    • Proposed as answer by sumitlathwal Tuesday, May 5, 2009 6:30 AM
    • Marked as answer by Mukesh Kumar Tuesday, May 5, 2009 6:33 AM
    Tuesday, May 5, 2009 6:30 AM
  • Thanks
    Tuesday, May 5, 2009 6:34 AM