【问题标题】:Code throwing WebException works in .NET 3.5 but not in 4.0代码抛出 WebException 在 .NET 3.5 中有效,但在 4.0 中无效
【发布时间】:2012-03-14 15:23:45
【问题描述】:

我正在编写代码来检查文件是否存在。如果文件存在,它应该抛出一个 WebException。代码如下;如果我在 .NET 3.5 中运行此代码,则会引发 WebException,但如果使用 .NET 4.0,则不会引发异常。我希望抛出异常,以便验证文件是否存在。

bool IsExists = true;
try
{
    // string ftpServer = "FTP://111.111.111.111/16FebTo15Mar/Backup/MT_Backup/";
    string userName = "gff";
    // string filename = "sachin";
    string password = "gff@123";
    string ftppath= "FTP://111.111.111.111/16FebTo15Mar/Backup/MT_Backup/bh/"; 
                    // Ftpurl + new FileInfo(EmpFldr).Name + "/";

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftppath);
    request.Credentials = new NetworkCredential(userName, password);
    request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
    // request.Method = WebRequestMethods.Ftp.MakeDirectory;
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    IsExists = false;
}
return IsExists;

【问题讨论】:

标签: c# asp.net .net-4.0 ftp ftpwebrequest


【解决方案1】:

如果您想通过 FTP 检查文件是否存在,请使用check out this answer

我建议改用以下 WebRequestMethod,这适用于我测试的所有服务器,即使是不会返回文件大小的服务器。

WebRequestMethods.Ftp.GetDateTimestamp

【讨论】:

  • 另外一个想法是使用request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 并解析列表来查找苍蝇。由于并非所有 FTP Web 请求类型都由 Microsoft 正确实现。
猜你喜欢
  • 2012-09-05
  • 2014-07-10
  • 2016-02-24
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2019-05-18
相关资源
最近更新 更多