title: c# ftp upload to Linux
body: <p>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 <a href="ftp://webserver/Logs" rel="nofollow">ftp://webserver/Logs</a> 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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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... :-D  Any ideas warmly welcomed?</p>
  
  <p><strong>App code</strong></p>
  
  <pre><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;
  }
  </code></pre>
  
  <p><strong>Filezilla log via Wireshark</strong></p>
  
  <pre><code>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.
  </code></pre>
  
  <p><strong>App log via Wireshark</strong></p>
  
  <pre><code>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.
  </code></pre>
  
  <hr>
  
  <p>It creates the folders if they dont exist.</p>
  
  <pre><code>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.
  </code></pre>