Check if directory exists on FTP server
12,890
I successfully solved this issue by changing my directory to be:
directory = @"ftp://ftp.example.com/Rubicon/";
![PiousVenom](https://i.stack.imgur.com/8ZYZ5.png?s=256&g=1)
Author by
PiousVenom
Updated on July 20, 2022Comments
-
PiousVenom almost 2 years
I'm running a check to see if a directory exists on my FTP server:
public bool DirectoryExists(string directory) { bool directoryExists; var request = (FtpWebRequest)WebRequest.Create(directory); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("user", "pass"); try { using (request.GetResponse()) { directoryExists = true; } } catch (WebException) { directoryExists = false; } return directoryExists; }
In this case:
directory = @"ftp://ftp.example.com/Rubicon";
On my server, I have a folder named
Rubicon1
. This is causing my check to returntrue
. How can I ensure that it fails unless it matches the directory name exactly?