I have here a textbox that should accept a file path. How should I validate (by a button click) whether the given path file do exists or not?
For example, "C:MyDocs\\sample.txt" should be invalid because it is not actually existing in my local drive and there's no '\\' after 'C:'..
i have tried using this:
FileInfo fi = new FileInfo(fName);
if (fi.Exists)
//do something
but it doesn't satisfy my issue..can anyone advise?
if(!File.Exists(filename))
{
// file does not exist or path is invalid
}
use Uri.IsWellFormedUriString(path, UriKind.Absolute);
to check if the path is valid (beside if the file exists)
read here for Uri validation: http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx
Sample:
bool isValid = Uri.IsWellFormedUriString(fName, UriKind.Absolute) &&
File.Exists(fName);
if the result is true, you can know for sure that the file format supplied by the user is valid and the file exists @ the file system.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.