C # FTP with CD Off

I am trying to run the following code:

string url = String.Format(@"SOMEURL"); string user = "SOMEUSER"; string password = "SOMEPASSWORD"; FtpWebRequest ftpclientRequest = (FtpWebRequest)WebRequest.Create(new Uri(url)); ftpclientRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpclientRequest.UsePassive = true; ftpclientRequest.Proxy = null; ftpclientRequest.Credentials = new NetworkCredential(user, password); FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse; 

This works fine, but for one specific server this gives an error of 500: The syntax is not recognized. The Change Directory command is disabled on the problem server, and the site administrator told me that .NET issues the Change Directory command by default with all FTP connections. It's true? Is there any way to disable this?
EDIT: When I enter from the command line, I ended up in the correct directory:
ftp> pwd
257 "/" - current directory

+6
c # ftp
source share
5 answers

I just tested this on one of our development servers and there really is a CWD released by .NET FtpWebRequest:

  new connection from 172.16.3.210 on 172.16.3.210:21 (Explicit SSL)
 hostname resolved: devpc
 sending welcome message.
 220 Gene6 FTP Server v3.10.0 (Build 2) ready ...
 USER testuser
 testuser, 331 Password required for testuser.
 testuser, PASS ****
 testuser, logged in as "testuser".
 testuser, 230 User testuser logged in.
 testuser, OPTS utf8 on
 testuser, 501 Please CLNT first.
 testuser, PWD
 testuser, 257 "/" is current directory. 
  testuser, CWD / 
  testuser, change directory '/' -> 'D: \ testfolder' -> Access allowed. 
  testuser, 250 CWD command successful.  "/" is current directory.
 testuser, type I
 testuser, 200 Type set to I.
 testuser, PORT 172,16,3,210,4,127
 testuser, 200 Port command successful.
 testuser, NLST
 testuser, 150 Opening data connection for directory list.
 testuser, 226 Transfer ok.
 testuser, 421 Connection closed, timed out.
 testuser, disconnected.  (00d00: 05: 01)

This was even without specifying "/" in the uri when creating the FtpWebRequest object.

If you are debugging or viewing source code, the "FtpControlStream" class comes into play. See Call Stack:

  System.dll! System.Net.FtpControlStream.BuildCommandsList (System.Net.WebRequest req) Line 555 C #
 System.dll! System.Net.CommandStream.SubmitRequest (System.Net.WebRequest request = 
     {System.Net.FtpWebRequest}, bool async = false, bool readInitalResponseOnConnect = true) Line 143 C #
 System.dll! System.Net.FtpWebRequest.TimedSubmitRequestHelper (bool async) Line 1122 + 0x13 bytes C #
 System.dll! System.Net.FtpWebRequest.SubmitRequest (bool async = false) Line 1042 + 0xc bytes C #
 System.dll! System.Net.FtpWebRequest.GetResponse () Line 649 C #

The BuildCommandsList () method is called here. BuildCommandsList () creates a list of commands to send to the FTP server. This method has the following code snippet:

 if (m_PreviousServerPath != newServerPath) { if (!m_IsRootPath && m_LoginState == FtpLoginState.LoggedIn && m_LoginDirectory != null) { newServerPath = m_LoginDirectory+newServerPath; } m_NewServerPath = newServerPath; commandList.Add(new PipelineEntry(FormatFtpCommand("CWD", newServerPath), PipelineEntryFlags.UserCommand)); } 

When connecting to the server for the first time, m_PreviousServerPath is always null, the value of newServerPath is "/" and calculated by a function named GetPathAndFileName () (several lines are called before this code block). GetPathAndFileName () evaluates newServerPath as "/" if no path is specified, or if "/" is explicitly attached to the end of "ftp: // ...." uri.

Thus, this ultimately leads to the addition of the CWD command to the command pipeline, because null! = "/".

In a nutshell, unfortunately, you cannot override this behavior because it is burned in the source.

+11
source share

Although the post, how long ago ... no matter, I will give an answer here.

Instead of using ftp://server/path as the uri, try ftp://server/%2fpath/ .

Added %2f "is just an escaped / , adding that C # will treat the whole path as absolute. Or C # will go to ftp://server/ with the username, go to the user's home folder, then cd to the specified path, so your path will become user_home_path/path , which may be undesirable.

Additional information can be found at msdn http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

Hope this helps.

+1
source share

I think we had a similar problem, but I don’t remember the exact data.

To prevent the cd.net command from being issued, check if the default directory setting is specified for the user you are logging in to as specified in the directory in which you want to work. You can simply use the ftp command line client to check this out.

0
source share

Here is the solution: use this free open source FTP client library for C # made by Dan at C-SharpCorner.com: http://www.c-sharpcorner.com/uploadfile/danglass/ftpclient12062005053849am/ftpclient.aspx

Here is a sample code to download the file:

 FtpClient ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword); ftp.Login(); ftp.Upload(@"C:\image.jpg"); ftp.Close(); 

This library works great out of the box, but can also be easily expanded and modified.

0
source share

Using the information above, this worked for me.

Sends CWD - ftpState.ftpRequest = GetRequest (" ftp://192.168.0.2/tmp/file2download ")

Does not send CWD - ftpState.ftpRequest = GetRequest (" ftp://192.168.0.2//tmp/file2download ") pay attention to // after the server IP address (or name)

Dotnet version 2.0

 Private Function GetRequest(ByVal URI As String) As FtpWebRequest 'create request Dim result As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest) Return result End Function 
0
source share

All Articles