c# - Code to Determine if FTP Directory Exists Suddenly Stopped Working -
I wrote the following code for a long time to determine whether an FTP directory exists:
Public Child Directory Axis (String Directory) {try {FtpWebRequest Request = GetRequest (Directory); request. Method = WebRequestMethods.Ftp.List directory; (FtpWebResponse response = FtpWebResponse) as request.GetResponse () {StreamReader sr = New StreamReader (response.GetResponseStream (), System.Text.Encoding.ASCII); Sr.ReadToEnd (); sr.Close (); Response.Close (); } Back true; } Hold {} return false; } Secure FtpWebRequest GetRequest (string file name = "") {FtpWebRequest request = WebRequest.Create (_host.GetUrl (filename)) as FtpWebRequest; request. Authentication = new network credentials (username, password); request. Proxy = faucet; request. KeepAlive = false; Return request; } This code has worked for many years, but today it is not so. When testing a directory that does not exist, the code DirectoryExists () now throws an exception, and returns the method incorrectly true . If I assign the results of string of sr.ReadToEnd () , this is an empty string. In this situation, the code _host.GetUrl (filename) is "expected" back and still, my DirectoryExists () method does not throw exceptions when this path does not exist on the server is. I have passed this non-existent directory to any method that uses WebRequestMethods.Ftp.ListDirectoryDetails to create a directory list. This method gives only an empty list and does not throw any exceptions. I believe that when I took my code on a new computer with Visual Studio 2013, I had to face this problem first. I am using .NET 4.5 similar behavior when using .NET 4.5.1.
Question:
-
Why is not this code, which works for years and uses the same technology I used in most online examples I found , Will you do work? And what could possibly be the reason for this code to stop working?
-
Is there a way to detect the presence of a directory that works? I think the second way is to scan the root directory, though the logic will need to be separated when the rootin must confirm the root directory.
I managed to recreate my error on another site, On which I have access. After playing around something, this is my conclusion: -
When you not / with a FtpWebRequest Code>, such as: ftp://ftp.someftp.com/somefolder/invalidfolder and you specify WebRequestMethods. FTP as a method. List directory [/ code>], what's behind the scene to run the following command: NLST "somefolder / invalidfolder" Normally , The NLST will list the contents of the specified folder, and throws an exception if the folder does not exist but because you did not specify / at the end of the invalidfolder , the NLST seems to be That the invalidfolder can actually be a file (or filename pattern). If it manages to search for a folder called invalidfolder , then only it will be treated as a folder, otherwise it will be treated as the default file somefolder under invalidfolder to search for a file. If the file does not exist, then one of the following, depending on the FTP server software (and its configuration) is running: - It throws an error 550: file or Folder not found. (Example: spftp v1.0 )
- This returns an empty result (example: vsFTPd v2.0.5 )
In your case, the FTP server returns the latter response, and your code drops.
Solution? Just to ensure that the FTP folder you are trying to use, always enter a verification code, finally / is something like the following: - if (! Directory.EndsWith ('/')) directory = '/';
Comments
Post a Comment