Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:58:25+00:00 2026-05-26T15:58:25+00:00

Im trying to check if a directory exists on an FTP server. Before you

  • 0

Im trying to check if a directory exists on an FTP server. Before you say “use ListDirectory” or “use PrintWorkingDirectory”, they dont always work; for example, I tested if ftp://webserver/Logs existed and both told me it does when it actually doesnt. So Ive gone down the route of uploading a file to the directory and if successful, then the directory exists.

The problem is, the below method doesnt work with GoDaddy’s CentOS based server’s running vsFTPd 2.0.7.2. Works fine with Microsoft FTP server on IIS7.5.

So I monitored the traffic with Wireshark and used Filezilla to see what it was doing that my App wasnt to make it work. And the only difference is Filezilla is changing the working directory where as I am trying to upload the file with a path before it.

I have a feeling its something to do with the path its uploading to the server and the interpretation by Linux, cause it can be a bit funny with names… 😀 Any ideas warmly welcomed?

App code

private bool DirectoryExists(string d)
{
    bool exists = true;
    try
    {
        string file = "directoryexists.test";
        string path = url + homepath + d + "/" + file;

        //Try to save to the directory
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.UploadFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;

        byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE");
        req.ContentLength = fileContents.Length;

        Stream s = req.GetRequestStream();
        s.Write(fileContents, 0, fileContents.Length);
        s.Close();

        //Delete file if successful
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.DeleteFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;

        res = (FtpWebResponse)req.GetResponse();
        res.Close();
    }
    catch (WebException ex)
    {
        exists = false;
    }
    return exists;
}

Filezilla log via Wireshark

Response: 230 Login successful.
Request: CWD /Home/test1
Response: 250 Directory successfully changed.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,71,209)
Request: LIST
Response: 150 Here comes the directory listing.
FTP Data: 78 bytes
Response: 226 Directory send OK.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,177,1)
Request: STOR directoryexists.txt
Response: 150 Ok to send data.
Response: 226 File receive OK.

App log via Wireshark

Response: 230 Login successful.
Request: OPTS utf8 on
Response: 501 Option not understood.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,217,87)
Request: STOR test1/directoryexists.txt
Response: 553 Could not create file.

It creates the folders if they dont exist.

Response: 230 Login successful.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,220,60)
Request: STOR Logs/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs
Response: 257 Create folder operation successful.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,255,245)
Request: STOR Logs/LogFiles/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs/LogFiles
Response: 257 Create folder operation successful.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:58:25+00:00Added an answer on May 26, 2026 at 3:58 pm

    Linux bites again…

    The solution is to set a double slash in the path name so that when it comes to STOR, it has a leading slash… like so:

    string url = "ftp://website/";
    string homepath = "/Home/";
    string d = "test1";
    string file = "directoryexists.test";
    
    string path = url + homepath + d + "/" + file;
    

    so the complete path will look like ftp://website//Home/test1/directoryexists.test

    req = (FtpWebRequest)WebRequest.Create("ftp://website//Home/test1/directoryexists.test"); 
    

    That way the STOR command will look like

    STOR /Home/test1/directoryexists.test
    

    You can get the Home path from StatusDescription

    req = (FtpWebRequest)WebRequest.Create(url);
    req.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
    if (nc != null) req.Credentials = nc;
    if (cbSSL.Checked) req.EnableSsl = true;
    req.Timeout = 10000;
    res = (FtpWebResponse)req.GetResponse();
    
    System.Text.RegularExpressions.Regex regexp = new System.Text.RegularExpressions.Regex("\\s\"([^\"]*)\"\\s");
    homepath = regexp.Match(res.StatusDescription).Groups[1].Value;
    
    res.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
I'm trying to check if a service exists on a remote machine using the
I need to do a check to see if the file exists that they
I'm trying to check if a file exists or not in Tcl, but I
I am trying to deploy a Maven site to an FTP server. I am
I am trying to verify if a directory exists prior to moving a file
Can some on help me out here? I am trying check each directory entry
I am trying to get my code to work. I basically check that if
I'm trying to check, using an automated discovery tool, when JAR files in remote
I was trying to check out a project from SVN using Eclipse. I tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.