-4

Is there a way to verify a valid path / file name? The file name is known as it is given to the customer. It is unknown where the .exe and other file are placed.

ttom
  • 985
  • 3
  • 12
  • 21
  • _What_ step do you want to avoid? The whole method or only part of it? Probably a duplicate: http://stackoverflow.com/questions/6198392/check-whether-a-path-is-valid – Tim Schmelter Feb 10 '15 at 14:53
  • 1
    What do you mean by "correct"? Do you mean that it *could* be a valid file name, i.e. it contains no illegal characters? Or do you mean that the file actually exists (which, keep in mind, could change at any moment, so this wouldn't be the right approach anyway)? – rory.ap Feb 10 '15 at 14:54
  • I mean the path may be incorrect. This code will be used by others, and the needed file may be in a different location then on my computer. – ttom Feb 10 '15 at 14:56
  • 1
    @ttom -- You still haven't clarified anything. Re-read my comment. – rory.ap Feb 10 '15 at 14:56
  • Assume the file name is a valid file name. It does exist. It is unknown where the file is located. – ttom Feb 10 '15 at 14:58
  • 2
    @ttom How can you know a file exists if you don't know it's location? – juharr Feb 10 '15 at 15:02
  • 2
    Your best bet here is to stop asking the question entirely. Instead *do the operation on the file you wish to do, and handle the exception appropriately if it fails*. Suppose the file exists when you call File.Exists and then is deleted before you open it; you need to handle the exception for that case, so why do the "exists" call in the first place? Just handle the exception. Suppose the file exists but the user doesn't have permission to manipulate it; you need to handle the exception in that case, so *just handle the exception*. – Eric Lippert Feb 10 '15 at 16:21

2 Answers2

1

To check whether a file exists at a specified path, use System.IO.File.Exists(string path):

if (File.Exists(pathFileName))
{
    ...
}
else
{
    ...
}

To check whether a path or file name is valid (I.E. contains no illegal characters) use System.IO.Path.GetInvalidPathChars() or System.IO.Path.GetInvalidFileNameChars():

if (Path.GetInvalidFileNameChars().Any(c => pathFileName.Contains(c)))
{
    ...
}
Rik
  • 28,507
  • 14
  • 48
  • 67
0

Since your attempting to validate if a path actually exist, you should be able to use System.IO.

var path = "...";
if(Directory.Exist(path))
{
     // Valid / Exist
}

You can also validate a file as well, by simply utilizing File.Exist. Information can be found here.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. Trailing spaces are removed from the end of the path parameter before checking whether the directory exists. The path parameter is not case-sensitive. If you do not have at a minimum read-only permission to the directory, the Exists method will return false. The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Greg
  • 11,302
  • 2
  • 48
  • 79