2015-10-15 56 views
0

我正在使用ASP.NET MVC 5.1網站,如果它存在於ftp服務器中,我需要顯示一張圖片(CDN )。文件夾名稱和文件名與規則綁定,所以我事先知道它們。如果此文件不存在,我需要顯示一條消息。ASP.NET MVC - 如何檢查一個文件是否存在於FTP服務器

如何檢查此文件是否存在?

Suggested duplicate (Verify if file exists or not in C#)沒有幫助,因爲我需要檢查,如果該文件遠程服務器,而不是一個本地文件夾存在。

+1

string destination = "ftp://something.com/"; string file = "test.jpg"; string extention = Path.GetExtension(file); string fileName = file.Remove(file.Length - extention.Length); string fileNameCopy = fileName; int attempt = 1; while (!CheckFileExists(GetRequest(destination + "//" + fileNameCopy + extention))) { fileNameCopy = fileName + " (" + attempt.ToString() + ")"; attempt++; } // do your upload, we've got a name that's OK } private static FtpWebRequest GetRequest(string uriString) { var request = (FtpWebRequest)WebRequest.Create(uriString); request.Credentials = new NetworkCredential("", ""); request.Method = WebRequestMethods.Ftp.GetFileSize; return request; } private static bool checkFileExists(WebRequest request) { try { request.GetResponse(); return true; } catch { return false; } } 

參考這是你的樣品的問題:在這裏輸入鏈接的描述(http://stackoverflow.com/questions/347897/how-to- check-if-file-exists-on-ftp-before-ftpwebrequest) –

回答

相關問題