c# - How to detect whether a file exists? -
can 1 suggest me best way see if file exists? file.exists not working me.
string abc = "me_label.deploy"; file.exists(abc) the file, abc, coming streamreader.readline();.
i used full path. don't want include files in project. if include in project working fine.
my code:
ftpwebrequest ftp = (ftpwebrequest)ftpwebrequest.create("me@localhost/"; + ab); //ab=newfolder/newfolder1 ftp.credentials = new networkcredential(user, pass); ftp.method = webrequestmethods.ftp.listdirectory; streamreader sr = new streamreader(ftp.getresponse().getresponsestream()); stringbuilder result = new stringbuilder(); string abc = sr.readline(); while (abc != null) { result.append(abc); //abc=file result.append("\n"); if (file.exists(ab+abc)) { //file } }
any relative path in .net default relative bin/debug subdirectory of project. so, unless me_label.deploy in folder, program not able find it. if include it, copied folder , works.
but have make path absolute instead. use methods path class.
string filename = "me_label.deploy"; string basepath = @"c:\my documents\"; string absolutepath = path.combine(basepath, filename); // c:\my documents\me_label.deploy console.writeline(file.exists(absolutepath));
Comments
Post a Comment