检查FTP上是否存在文件不起作用 [英] Check if file exists on FTP not working

查看:118
本文介绍了检查FTP上是否存在文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将文件上传到FTP.即使在上传之前,我也需要检查FTP中是否存在文件.我正在使用此方法检查文件是否存在

Hi,

I am trying to upload files to FTP. Even before uploading, I need to check if the files exists in FTP. I am using this method to check if the file exists

private bool FtpDirectoryExists(string dirPath, string FTPUser, string FTPPassword)
       {
           bool IsExists = true;
           try
           {
               FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);
               request.Credentials = new NetworkCredential(FTPUser, FTPPassword);
               request.Method = WebRequestMethods.Ftp.ListDirectory;
               FtpWebResponse response = (FtpWebResponse)request.GetResponse();
               //response.Close();
           }
           catch (WebException ex)
           {
               IsExists = false;
           }
           return IsExists;
       }


即使文件不存在,它也总是返回true.任何人都可以帮我吗


It always returns true, even if the file doesnt exist..Can anyone please help me out

推荐答案

在这里看看

如何检查文件是否存在于FTP服务器 [ ^ ]

该提示中的代码

Have a look here

How to check if a file exists on an FTP server[^]

Code from that tip

public bool CheckIfFtpFileExists(string fileUri) 
{ 
    FtpWebRequest request = WebRequest.Create(fileUri); 
    request.Credentials = new NetworkCredential("username", "password"); 
    request.Method = WebRequestMethods.Ftp.GetFileSize; 
    try 
    { 
        FtpWebResponse response = request.GetResponse(); 
        // THE FILE EXISTS 
    } catch(WebException ex) 
    { 
        FtpWebResponse response = ex.Response; 
        if (FtpStatusCode.ActionNotTakenFileUnavailable == response.StatusCode)
        { 
            // THE FILE DOES NOT EXIST 
            return false; 
        } 
     } 
     return true; 
}


这篇关于检查FTP上是否存在文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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