Working with Paths

In the previous examples, we’ve been dealing with user input to specify a path. If a path contains invalid characters, constructors like DirectoryInfo’s will throw an exception to signal such an issue.

Often, developers need to go beyond just taking a prepared path. While paths are just string values, manipulating them as such (for example, using plain old concatenation) isn’t a particularly good idea, especially when user input is involved. The chance of building an invalid path exists, so it’s better to let specialized functions do the job. For this purpose, the static methods on the Path class were introduced as wrappers around a set of APIs available in Win32.

One set of methods deals with breaking up a path in its constituents, ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.