ProblemYou have a file path supplied by the user, but you need to verify that it is valid before using it. SolutionUse 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 DiscussionIf 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 AlsoSeveral of the recipes in this chapter use FileExists() before attempting access to a user-specified path. Recipe 12.2 discusses the DirectoryExists() method. |