如何检查ftp服务器上是否存在指定的文件夹 [英] How to check that a specified folder exists on ftp server

查看:424
本文介绍了如何检查ftp服务器上是否存在指定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我正在制作上传多个文件和文件夹的上传器,所以我需要检查ftp服务器上是否存在特定文件夹? ????如果它不存在,我们调用MakeDir()函数创建它,如果存在,我们将离开它.........所以为了实现这一点,我已经搜索了很多,我已经得到了下面提到的功能,它有一个问题,即任一目录是否存在它将返回true。

Dear All,

I am making a uploader which uploads multiple files and folder so i need to check that a specific folder exists on ftp server or not??????If it doesn't exist we call the MakeDir() function to create it and if it exist we'll leave it.........So to achieve this i've search pretty much and i've got the below mentioned function and it have a problem that either directory exists or not it will return "true."

private void btnCheck_Click(object sender, EventArgs e)
        {
            bool result = FtpDirectoryExists("ftp://micro123/First", "anonymous", "anonymous");
            MessageBox.Show("Folder"+result);
        }
        public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
        {
            bool IsExists = true;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                IsExists = false;
            }
            return IsExists;
        }





此外,我还尝试了下面提到的,如果文件夹存在与否,它将返回false。 />



Moreover, I've also tried the below mentioned which will return false if folder exist or not.

request.Method = WebRequestMethods.Ftp.GetDateTimestamp;





希望你们这些人了解我的问题..........请任何人帮助我。:(



Hope you guys understand my problem..........Please anyone help me.:(

推荐答案

这是非常混乱的。



http://www.example-code.com/csharp/ftp_dirExists.asp [ ^ ]



似乎使用了一个自定义组件,但你可以在那里看看他们是否这样做。你也可以尝试浏览目录,看看你得到了什么,或创建在任何情况下,如果它不会清空它。
This is pretty messy.

http://www.example-code.com/csharp/ftp_dirExists.asp[^]

seems to use a custom component, but you could look in there to see hwo they do it. You could also just try browsing to the directory, and see what you get back, or creating it in any case, if that doesn't empty it.


一种方法是:使用适当的列出目录System.Net.FtpWebRequest.Met hod 。该方法可以是 System.Net.WebRequestMethods.Ftp.ListDirectory System.Net.WebRequestMethods.Ftp。 ListDirectoryDe​​tail 。读取HTTP响应 System.Net.FtpWebResponse 中的数据,查看哪个目录存在,哪个目录不存在。



请参阅:

http://msdn.microsoft .com / zh-cn / library / system.net.ftpwebrequest.method.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.net .ftpwebresponse.aspx [ ^ ]。



-SA
One way is: list directories using appropriate System.Net.FtpWebRequest.Method. The method can be System.Net.WebRequestMethods.Ftp.ListDirectory or System.Net.WebRequestMethods.Ftp.ListDirectoryDetail. Read the data in HTTP response System.Net.FtpWebResponse to see which directory exists and which is not.

Please see:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.method.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.aspx[^].

—SA


大家好,



我通过使用
Hi All,

I've solved this problem by using
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;



实际上我通过使用上面的webrequest方法从FTP服务器获取所有文件和文件夹并将它们添加到列表框中,然后使用 foreach 如果存在则逐个循环检查。



希望你理解。


Actually I am getting all the files and folders from FTP server by using above webrequest method and add them into list box and after that by using foreach loop check one by one if it exist or not.

Hope you understand.


这篇关于如何检查ftp服务器上是否存在指定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆