【发布时间】: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;
【问题讨论】:
-
不太确定,但请尝试 changing the
UsePassiveproperty 获取 ftprequest -
是否还在抛出异常?捕捉更一般的异常,看看它是什么。
标签: c# asp.net .net-4.0 ftp ftpwebrequest