判断服务器文件存在,C# 判断服务器上文件是否存在

var path = " FramePath" ;

var filePath = Server.MapPath(path);

if (System.IO.File.Exists(filePath))

{

var fileName = Path.GetFileName(filePath);

FileInfo info = new FileInfo(filePath);

Response.Clear();

Response.ClearHeaders();

Response.Buffer = false;

Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FrameName + ".zip", System.Text.Encoding.UTF8).Replace("+", "%20"));

Response.AppendHeader("Content-Length", info.Length.ToString());

Response.WriteFile(filePath);

Response.Flush();

Response.End();

return new EmptyResult();

}

原文:http://www.cnblogs.com/etong/p/4353656.html

  • 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、付费专栏及课程。

余额充值