Recipe 11.17 Verifying a Path
Problem
You have a
path-possibly entered by the user-and you need to verify
that it has no illegal characters and that a filename and extension
exist.
Solution
We use several of the static fields and methods in the
Path class. We begin by writing a method called
CheckUserEnteredPath, which accepts a string
containing a path entered by the user and a Boolean value to decide
whether we want to find all illegal characters or just the occurrence
of any illegal character. Just finding any illegal character is much
faster if you don't care which illegal characters
are present. This method first calls another method, either
FindAnyIllegalChars or
FindAllIllegalChars, each of which are described
later in the Solution. If there are no illegal characters in this
path, it is then checked for the existence of a file and
extension:
public bool CheckUserEnteredPath(string path, bool any)
{
try
{
Console.WriteLine("Checking path {0}",path);
bool illegal = false;
// two ways to do the search, one more expensive than the other...
if(any == true)
illegal = FindAnyIllegalChars(path);
else
illegal = FindAllIllegalChars(path);
if (!illegal)
{
if (Path.GetFileName(path).Length == 0)
{
Console.WriteLine("A filename must be entered");
}
else if (!Path.HasExtension(path))
{
Console.WriteLine("The filename must have an extension");
}
else
{
Console.WriteLine("Path is correct");
return (true);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString( ));
}
return (false);
}
The FindAllIllegalChars method, which is called by
the CheckUserEnteredPath method, accepts a string
containing a path. This path is checked for illegal characters by
using the IndexOfAny method on
the string class. The IndexOfAny method finds the
first occurrence of one of the characters supplied to it in the
string being looked at. This method uses the
Path.InvalidPathChars
static field to determine if any illegal characters exist in this
path:
private bool FindAllIllegalChars(string userEnteredPath)
{
int invalidCharPos = -1;
bool endOfPath = false;
bool foundIllegalChars = false;
while (!endOfPath)
{
invalidCharPos = userEnteredPath.IndexOfAny(Path.InvalidPathChars,
invalidCharPos + 1);
if (invalidCharPos == -1)
{
endOfPath = true;
}
else
{
foundIllegalChars = true;
Console.WriteLine("Invalid char {0} found at position {1}",
userEnteredPath[invalidCharPos],invalidCharPos);
if (invalidCharPos >= userEnteredPath.Length - 1)
{
endOfPath = true;
}
else
{
invalidCharPos++;
}
}
}
return (foundIllegalChars);
}
The FindAnyIllegalChars method, which is also
called by the CheckUserEnteredPath method, accepts
a string containing a user entered path. This path is checked for the
existence of any illegal characters by using the
IndexOfAny method on
the string class. If the IndexOfAny method finds
anything, we have an illegal path and we return
false:
private bool FindAnyIllegalChars(string userEnteredPath)
{
int invalidCharPos = userEnteredPath.IndexOfAny(Path.InvalidPathChars);
if (invalidCharPos == -1)
{
return (false);
}
else
{
Console.WriteLine("Invalid char {0} found at position {1}",
userEnteredPath[invalidCharPos],invalidCharPos);
return (true);
}
}
Discussion
This recipe provides a way of verifying a path for invalid characters
before it can be used in your application. This recipe does not
verify that the directory or path exists; use the
Directory.Exists or File.Exists
methods to perform this verification.
The CheckUserEnteredPath method starts by calling
the FindAnyIllegalChars or
FindAllIllegalChars methods and passing the chosen
one a path string. Two different mechanisms validate the path against
the set of characters supplied by
Path.InvalidPathChars . This field contains all of the invalid
characters that could be entered into a path string. Both methods
return true if there are illegal characters found,
but FindAnyIllegalChars prints information to the
console only for the first one found, whereas
FindAllIllegalChars prints out information for
every illegal character found.
See Also
See the "String Class" and
"Path Class" topics in the MSDN
documentation.
|