Recipe 12.10. Determining If a File Exists


Problem

You have a file path supplied by the user, but you need to verify that it is valid before using it.

Solution

Use the My.Computer.FileSystem. FileExists() method to determine whether a path string is a valid file or not:

 If (My.Computer.FileSystem.FileExists( _       userSuppliedPath) = True) Then    MsgBox("Invalid file specified.") Else    ' ----- Process file here. End If 

Discussion

If you wish to validate a directory instead of a file, use the equivalent DirectoryExists() method:

 If (My.Computer.FileSystem.DirectoryExists( _       userSuppliedPath) = True) Then    MsgBox("Invalid directory specified.") Else    ' ----- Process directory here. End If 

See Also

Several of the recipes in this chapter use FileExists() before attempting access to a user-specified path.

Recipe 12.2 discusses the DirectoryExists() method.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net