开发者

Dir exists or not on FTP

开发者 https://www.devze.com 2022-12-15 06:29 出处:网络
How do I check if some DIR exists on the server or not? Although I can check file exists or not through:

How do I check if some DIR exists on the server or not? Although I can check file exists or not through:

try
{
    FtpWebRequest request=null;

    request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl + "/somefile.txt");
    request.Credentials = new NetworkCredential(username, password);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
        // Okay.  
    }
}
catch (WebException ex)
{
    if (ex.Response != null)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        开发者_JAVA技巧if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            //task
        }
    }
}

But how do I check DIR? If I only specify DIR in URI then it doesn't go to catch if DIR doesn't exists.


request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl); //no file name
request.Credentials = new NetworkCredential(username, password);
myFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;

And check if your file/dir is listed.

You need to interrogate the response, it should contain a list of possible files and directorys.

You should not be using a catch to handle program flow. MSDN example


I don't think the code does what you think it does. As far as I understand the docs you're trying to get a ls (think dir in DOS/Windows, a list of files in a directory) for a file. That doesn't make sense. It works, somewhat, because you get the exception for trying to access a directory "somefile.txt".

You should be able to do it the right way (tm) by looking at the output of the ListDirectory response of the parent:

Do a ListDirectory ftp://yourserver/ and check if

  • your file
  • your directory

is listed.


I use:

private bool CreateFTPDirectory(string directory)
{

    try
    {
        //create the directory
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpURI+"/"+directory));
        requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
        requestDir.UsePassive = true;
        requestDir.UseBinary = true;
        requestDir.KeepAlive = false;
        //requestDir.UseDefaultCredentials = true;
        requestDir.Credentials = new NetworkCredential(UserId, Password);
        requestDir.Proxy = WebRequest.DefaultWebProxy;
        requestDir.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

        FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
        Stream ftpStream = response.GetResponseStream();

        ftpStream.Close();
        response.Close();

        return true;
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if ((response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) || (((int)response.StatusCode)==521))
        {
            response.Close();
            return true;
        }
        else
        {
            response.Close();
            return false;
        }
    }
}

This has the side effect of creating the directory as well. If it already exists you get a 521 result returned which isn't defined in the .NET Enum.

When you connect to an FTP server you might specify the Uri as "ftp//ftp.domain.com/somedirectory" but this translates to: "ftp://ftp.domain.com/homedirectoryforftp/somedirectory". To be able to define the full root directory use "ftp://ftp.domain.com//somedirectory" which translates to //somedirectory on the machine.


By using edtFTPnet

  private void ftp_folder_IsExists()
{
    FTPClient ftp = default(FTPClient);

    ftp = new FTPClient("ftp_host_name_here");  // ex.:- no need to use ftp in the host name, provide name only
    ftp.Login("username", "password");

    string[] file_and_folders = ftp.Dir(".", false);// . is used to get all the [files and folders] in the root of FTP

    string[] file_and_folders_1 = ftp.Dir("MyFolder", false);// this will get all the [files and folder] inside MyFolder (ex. ftp.ahostname.com/MyFolder/)


    //checking for a FILE
    if (file_and_folders.Contains("something.txt")) {
        //Do what you want..
    } else {
        //Do what you want..

    }

    //checking for a FOLDER
    if (file_and_folders.Contains("A_Folder")) {
        //Do what you want..
    } else {
        //Do what you want..

    }

}

Note : Code Written In VB.NET and Converted Using http://converter.telerik.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号