2012-02-24 2 views
2

파일이 존재하는지 여부를 확인하는 코드를 작성하고 있습니다. 파일이 존재하면 WebException을 던져야합니다. 코드는 아래와 같습니다. .NET 3.5에서이 코드를 실행하면 WebException이 throw되지만 .NET 4.0을 사용하면 예외가 throw되지 않습니다. 내가 파일의 존재를 확인할 수 있도록 던져 질 예외가 싶습니다.WebException을 throw하는 코드는 .NET 3.5에서는 작동하지만 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 = "[email protected]"; 
    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; 
+0

매우 확실하지만 [은'UsePassive' 속성을 변경]하지 (http://stackoverflow.com/questions/4604693/ftp-throws-webexception-only-in-net-4-0) ftprequest – V4Vendetta

+0

예외가 여전히 throw됩니까? 보다 일반적인 예외를 포착하고 그것이 무엇인지 확인하십시오. – rikitikitik

답변

0

FTP를 통해 파일의 존재 여부를 확인하려면 check out this answer입니다.

대신 다음 WebRequestMethod를 사용하는 것이 좋습니다.이 방법은 테스트 한 모든 서버에서 파일 크기를 반환하지 않는 경우에도 사용할 수 있습니다.

WebRequestMethods.Ftp.GetDateTimestamp 
+0

또 다른 아이디어는'request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; '를 사용하고 flie를 찾기 위해 구문 분석 목록을 사용하는 것입니다. 모든 FTP 웹 요청 유형이 Microsoft에 의해 적절하게 구현되지 않았기 때문입니다. – Marcin

+0

회신 해 주셔서 감사합니다. 작업 파일 ............ – LetMeIN

관련 문제