c# - ASP.NET MVC - How can I check if a file exists on an ftp server -
this question has answer here:
i'm working on asp.net mvc 5.1 web site, , need show picture if exists in ftp server (cdn). foldername , filename tied rules know them beforehand. need display message if file not exist.
how can check if file exists?
suggested duplicate (verify if file exists or not in c#) not since need check if file exists on remote server , not local folder.
try following code
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++; } // upload, we've got 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; } }
reference ftp check if file exist
Comments
Post a Comment