2015-10-15 84 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) –

回答

相关问题