Click here to Skip to main content
15,896,435 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Dear All,

I am making a uploader which uploads multiple files and folder so i need to check that a specific folder exists on ftp server or not??????If it doesn't exist we call the MakeDir() function to create it and if it exist we'll leave it.........So to achieve this i've search pretty much and i've got the below mentioned function and it have a problem that either directory exists or not it will return "true."
C#
private void btnCheck_Click(object sender, EventArgs e)
        {
            bool result = FtpDirectoryExists("ftp://micro123/First", "anonymous", "anonymous");
            MessageBox.Show("Folder"+result);
        }
        public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
        {
            bool IsExists = true;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                IsExists = false;
            }
            return IsExists;
        }


Moreover, I've also tried the below mentioned which will return false if folder exist or not.
C#
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;


Hope you guys understand my problem..........Please anyone help me.:(
Posted
Updated 13-Feb-12 11:24am
v2

This is pretty messy.

http://www.example-code.com/csharp/ftp_dirExists.asp[^]

seems to use a custom component, but you could look in there to see hwo they do it. You could also just try browsing to the directory, and see what you get back, or creating it in any case, if that doesn't empty it.
 
Share this answer
 
One way is: list directories using appropriate System.Net.FtpWebRequest.Method. The method can be System.Net.WebRequestMethods.Ftp.ListDirectory or System.Net.WebRequestMethods.Ftp.ListDirectoryDetail. Read the data in HTTP response System.Net.FtpWebResponse to see which directory exists and which is not.

Please see:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.method.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 13-Feb-12 18:26pm    
Good reply :)
Sergey Alexandrovich Kryukov 13-Feb-12 19:15pm    
Thank you, Espen.
--SA
zyck 19-Feb-12 2:23am    
nice solution! 5up!
Sergey Alexandrovich Kryukov 19-Feb-12 11:24am    
Thank you.
--SA
Hi All,

I've solved this problem by using
C#
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

Actually I am getting all the files and folders from FTP server by using above webrequest method and add them into list box and after that by using foreach loop check one by one if it exist or not.

Hope you understand.
 
Share this answer
 
v2
Comments
André Kraak 19-Feb-12 14:18pm    
Edited solution:
Added pre tags
You mod code: FtpDirectoryExists("ftp://micro123/First/", "anonymous", "anonymous");
Code working!
 
Share this answer
 
I found this method ineffective if you have a large number of files to upload into a directory already containing a large number of directories/files....

So I created an FTP class that calls a webpage on the server that does the search for the dir on the server and returns just a boolean. Far more efficient interms of bandwidth especially if you are using a dongle or have low bandwidth.
 
Share this answer
 

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



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