c#如何判断ftp服务器上文件夹是否存在,C#做FTP上传时,怎么判断 文件路径 是否存在?...

123456qqq

TA贡献1654条经验 获得超5个赞

1 private bool DirectoryExist(string URI)

2 {

3 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(URI));

4 reqFTP.Credentials = new NetworkCredential("", "");

5 reqFTP.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

6

7 reqFTP.UseBinary = true;

8 try

9 {

10 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

11 response.Close();

12 return true;

13 }

14 catch(Exception ex)

15 {

16 string e=ex.Message;

17 return false;

18 }

19 }

查看完整回答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,要判断 FTP 服务器上是否存在相同的文件,可以使用 FTP 协议中的 SIZE 命令获取文件大小,如果文件大小相同,则判断为相同文件。以下是 C# 的示例代码: ```csharp using System.Net; // 获取文件大小 public static long GetFileSize(string url, string username, string password) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Ftp.GetFileSize; request.Credentials = new NetworkCredential(username, password); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { return response.ContentLength; } } // 判断文件是否存在 public static bool FileExists(string url, long size, string username, string password) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Ftp.GetFileSize; request.Credentials = new NetworkCredential(username, password); try { using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { long fileSize = response.ContentLength; return fileSize == size; } } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { return false; } else { throw; } } } // 示例用法 string url = "ftp://ftp.example.com/path/to/file.txt"; string username = "username"; string password = "password"; long size = GetFileSize(url, username, password); bool exists = FileExists(url, size, username, password); if (exists) { Console.WriteLine("File exists."); } else { Console.WriteLine("File does not exist."); } ``` 请注意,上述代码仅供参考,具体实现可能因 FTP 服务器的不同而有所差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值