首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#检查是否存在FTP目录

问C#检查是否存在FTP目录
EN

Stack Overflow用户
提问于 2013-05-24 11:14:51
回答 3查看 15.6K关注 0票数 2

我有一个检查目录是否存在的FTP方法。它在开始时工作得很好,但是现在,即使目录不存在,它仍然返回true。我尝试了很多方法,并设置了断点,看看我可以使用response对象的什么属性来确定目录是否存在。我还在互联网上搜索了一下,这些解决方案似乎对我不起作用。这是我的FTP方法。

代码语言:javascript
复制
public bool directoryExists(string directory)
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                try
                {
                    using (ftpRequest.GetResponse())
                    {
                        return true;
                    }
                    //var response = ftpRequest.GetResponse();
                    //if (response != null)
                    //    return true;
                    //else return false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return false;
                }

                /* Resource Cleanup */
                finally
                {
                    ftpRequest = null;
                }
            }

下面是调用它并返回true的方法,即使该目录不存在:

代码语言:javascript
复制
private string getDirectory(ref FtpClass ftp, string internalID)
        {
            string remoteSubPathDel = internalID + "\\trunk\\prod\\xml\\delete";
            string remoteSubPathUpdate = internalID + "\\trunk\\prod\\xml\\update";
            string remoteSubPathNew = internalID + "\\trunk\\prod\\xml\\new";
            if (ftp.directoryExists(remoteSubPathDel))
                return remoteSubPathDel;
            else if (ftp.directoryExists(remoteSubPathUpdate))
                return remoteSubPathUpdate;
            else if (ftp.directoryExists(remoteSubPathNew))
                return remoteSubPathNew;
            else
                return String.Empty;
        }

希望有人能帮上忙。谢谢!:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-24 13:09:11

我有一个解决这个问题的办法。不是最好看的,但它很有效。

也许这可以帮助其他人解决和我一样的问题。

代码语言:javascript
复制
public bool directoryExists2(string directory, string mainDirectory)
        {
            try
            {
                var list = this.GetFileList(mainDirectory);
                if (list.Contains(directory))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

编辑:我包含了GetFileList方法来回应Ray Chang的评论

代码语言:javascript
复制
public string[] GetFileList(string path)
        {
            var ftpPath = host + "/" + path;
            var ftpUser = user;
            var ftpPass = pass;
            var result = new StringBuilder();
            try
            {
                var strLink = ftpPath;
                var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(strLink));
                reqFtp.UseBinary = true;
                reqFtp.Credentials = new NetworkCredential(ftpUser, ftpPass);
                reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFtp.Proxy = null;
                reqFtp.KeepAlive = false;
                reqFtp.UsePassive = true;
                using (var response = reqFtp.GetResponse())
                {
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        var line = reader.ReadLine();
                        while (line != null)
                        {
                            result.Append(line);
                            result.Append("\n");
                            line = reader.ReadLine();
                        }
                        result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    }
                }
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                Console.WriteLine("FTP ERROR: ", ex.Message);
                return null;
            }

            finally
            {
                ftpRequest = null;
            }
        }
票数 7
EN

Stack Overflow用户

发布于 2013-05-24 11:32:59

试试这个:

代码语言:javascript
复制
public bool directoryExists(string directory)
{
    /* Create an FTP Request */
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
    /* Log in to the FTP Server with the User Name and Password Provided */
    ftpRequest.Credentials = new NetworkCredential(user, pass);
    /* Specify the Type of FTP Request */
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    try
    {
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            return true;
        }
    }
    catch (Exception ex)
    {
        return false;
    }

            /* Resource Cleanup */
    finally
    {
        ftpRequest = null;
    }
}

注意这一行using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())

票数 1
EN

Stack Overflow用户

发布于 2021-06-22 06:14:55

这适用于我们的Linux和Windows服务器读取的live盒(在Windows和Linux上)

备注

Windows ftp上的主文件夹是web

  • the,Linux ftp上的主文件夹是public_html

TL;DR;

  • 的底线:网址需要以/

结尾

:It works

代码语言:javascript
复制
ftp://ftp.yourdomain.com.br/public_html/
ftp://ftp.yourdomain.com.br//public_html/
ftp://ftp.yourdomain.com.br/web/
ftp://ftp.yourdomain.com.br//web/

:It不起作用

代码语言:javascript
复制
ftp://ftp.yourdomain.com.br/public_html
ftp://ftp.yourdomain.com.br//public_html
ftp://ftp.yourdomain.com.br/web
ftp://ftp.yourdomain.com.br//web

用法://验证public_html目录是否存在

代码语言:javascript
复制
var url = "/public_html/";
var result = FtpUtil.DoesDirectoryExists(url,  "ftp://ftp.yourdomain.com.br", "ftp user here", "ftp password here");

代码语言:javascript
复制
static bool DoesDirectoryExists(string directory, string ftpHost, string ftpUser, string ftpPassword) {
                    FtpWebRequest ftpRequest = null;
                    try {
                            ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpHost + directory));
                            ftpRequest.Credentials = new NetworkCredential(ftpUser, string ftpPassword);
                            ftpRequest.UseBinary = true;// optional
                            ftpRequest.KeepAlive = false;// optional
                            ftpRequest.UsePassive = true;// optional
                            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        
                            using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) {
                                return true;//directory found
                            }
                    }
                    catch (WebException ex) {
                            if (ex.Response != null) {
                                FtpWebResponse response = (FtpWebResponse)ex.Response;
                                if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                                    return false;// directory not found.  
                            }
                            return false; // directory not found.
                    }
                    finally {
                        ftpRequest = null;
                    }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16727157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档