4

I am using FTP to upload files to a folder on an FTP server folder, but first I need to determine that the folder exists. MSDN contains an example of how to use FtpWebResponse to check if a folder exists:

public class WebRequestGetExample
{
    public static void Main ()
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();
    }
}

Using the code above, one can obtain a list of folders off the root level of the server, and I can do this in my own situation. That is NOT my question.

My question is, what if the folder I am wanting to write to is a SUBFOLDER of one of the folders that results from the call to ListDirectoryDetails? How do I dig deeper into the folder structure?

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • @MartinPrikryl, the original text of the question made it clear that I already knew about how to check if an FTP directory exists. I've changed the question to make it even more clear that my question is asking how to check for the existence of a subfolder of one of the root subfolders. – Cyberherbalist Jun 08 '16 at 15:56
  • No, your code does not check, if any directory exists, it just lists a root directory. See the answers to the linked question for an actual code to check, if some directory exists. – Martin Prikryl Jun 08 '16 at 16:03
  • Sorry, @MartinPrikryl, the linked question has an answer that does not work properly under some circumstances -- and attempting to create a folder while trapping an exception is not a good way to do it in any case. One of my answers below also clarified it for me. Was also dealing with a shared folder on the server in question, which complicated matters. – Cyberherbalist Jun 09 '16 at 15:29

2 Answers2

1

Let's say you want to use the subfolder temp. Then you have to append the folder to your URI like this:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/temp/somefile.xyz");

In this way you can access subfolders, subsubfolders and so on.

falx
  • 139
  • 3
  • 13
1

You can try this:

try 
{  
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://address/subfolder/subfolder");  
    request.Method = WebRequestMethods.Ftp.ListDirectory;  
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
    {  
        // Directory exists, you can work on it.
    }  
}  
catch (WebException ex)  
{  
    if (ex.Response != null)  
    {  
        FtpWebResponse response = (FtpWebResponse)ex.Response;  
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)  
        {  
            // Directory not found.  
        }  
    }  
} 

This way, you are basically checking if you can list the directory by calling Ftp.ListDirectory as an FTP request method. If it fails, then your directory does not exist.

Kamil Solecki
  • 1,106
  • 20
  • 34
  • That did it. My problem was that the server had a public shared folder named "FTPDATA", and that was the highest visible folder -- and when I included its name in the path it couldn't find it. This is clearly because it was interpreted as the root folder, name ignored. Leaving "FTPDATA" out of the path enabled me to find the subfolders. – Cyberherbalist Jun 09 '16 at 15:22
  • Im glad to have helped :) – Kamil Solecki Jun 09 '16 at 15:23
  • Not only you have answered a duplicate question, instead of voting it to be closed as duplicate. But you have posted the same answer as already exists for the duplicate question, without giving any credit to it. – Martin Prikryl May 29 '18 at 07:36