VB.NET - Checking FTP directory exists always returns true -
can tell me why function below returns true, if ftp directory in question not exist?
the value of directoryurl pass in of form:
ftp://ip_address/directory/subdirectory/
and has trailing forward slash.
public function doesdirectoryexist(directoryurl string) boolean ' check target url formatted if not directoryurl.startswith("ftp://") directoryurl = "ftp://" & directoryurl ' create web request dim request ftpwebrequest = directcast(webrequest.create(directoryurl), ftpwebrequest) request.credentials = new networkcredential(_username, _password) request.method = webrequestmethods.ftp.listdirectory ' try , list contents of directory try using response ftpwebresponse = directcast(request.getresponse(), ftpwebresponse) ' have been succesful directory exists return true end using catch ex webexception dim response ftpwebresponse = directcast(ex.response, ftpwebresponse) if response.statuscode = ftpstatuscode.actionnottakenfileunavailable return false else throw new applicationexception("unable determine if ftp directory exists.") end if end try end function
weird. works me (i don't cast request guess shouldn't matter). code rely on:
dim response ftpwebresponse = request.getresponse() using (response) found = true end using
the alternative have reading list of directories:
using sr new system.io.streamreader(response.getresponsestream()) using sw new system.io.streamwriter("tempfile", false) sw.write(sr.readtoend()) end using end using
in worst scenario, should tackle problem (e.g., founds directory called "ghost", might use trigger not-found).
Comments
Post a Comment