c# - Check file existence before WebClient.DownloadFile -
i know how check if file exists before downloading.
current code:
string url = environment.getfolderpath(environment.specialfolder.desktop) + "/text.txt"; string path = "asdf.wix.com/text.txt"; using (var client = new webclient()) { client.downloadfile(url, path); }
the code works if file missing on site creates empty text.txt causes issues.
any ideas? thank you!
if url var points location on pc, check existence system.io.file.exists:
if(!system.io.file.exists(url) { //code handles file dne case.. maybe log , return? }
if it's pointing towards remote location, i'm not sure how check existence beforehand.
you could, however, handle 404 case returned webclient , delete erroneous text.txt file
using (var client = new webclient()) { try { client.downloadfile(url, path); } catch (webexception e) { var statuscode = ((httpwebresponse) e.response).statuscode; if (statuscode == httpstatuscode.notfound && system.io.file.exists(path)) { system.io.file.delete(path); //maybe log occurence } } }
Comments
Post a Comment