
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
722
|
Chapter 12: Filesystem I/O
Discussion
This recipe provides a way of screening 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. The path is
validated against the set of characters supplied by both the
Path.GetInvalidPathChars
and the Path.GetInvalidFileNameChars static methods. These methods return charac-
ter arrays that contain all of the invalid characters that could be entered into a path
or filename string, respectively.
The
CheckUserEnteredPath and FindAnyIllegalChars methods return true if there are
illegal characters found.
FindAnyIllegalChars prints information to the console for
only the first one found, whereas
FindAllIllegalChars returns a List<char> contain-
ing all illegal characters found.
See Also
See the “String Class” and “Path Class” topics in the MSDN documentation.
12.18 Using a Temporary File in Your Application
Problem
You need a temporary file in which to store information. This file should exist only
as long as the process that created it remains running.
{
Console.WriteLine( ...