The elusive Path class and it’s “hidden” static methods – level 200

If you ever find yourself manually parsing a file name or any part of a path, stop!  It’s very tempting to use a couple of lines of code to trim an extension off of a file, but why do it.  Also, if you ever have a directory path and a file name, don’t concatenate the two strings together (making sure that a “/” is in between), us Path.Combine instead.  I think that a lot of code can be reduced by reusing the BCL.  Every now and then you might save 15 minutes by taking 5 minutes to research what is available in the BCL. 


Let’s take a look at the System.IO.Path class and see what it can do for us.  Just by looking at the following method names, I’m sure you can figure out how to use them to reduce your codebase:


Public Fields



















public fieldstatic (Shared in Visual Basic)AltDirectorySeparatorChar

Supported by the .NET Compact Framework.

Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.
public fieldstatic (Shared in Visual Basic)DirectorySeparatorChar

Supported by the .NET Compact Framework.

Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.
public fieldstatic (Shared in Visual Basic)InvalidPathChars

Supported by the .NET Compact Framework.

Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.
public fieldstatic (Shared in Visual Basic)PathSeparator

Supported by the .NET Compact Framework.

A platform-specific separator character used to separate path strings in environment variables.
public fieldstatic (Shared in Visual Basic)VolumeSeparatorChar

Supported by the .NET Compact Framework.

Provides a platform-specific volume separator character.

Public Methods








































public methodstatic (Shared in Visual Basic)ChangeExtension

Supported by the .NET Compact Framework.

Changes the extension of a path string.
public methodstatic (Shared in Visual Basic)Combine

Supported by the .NET Compact Framework.

Combines two path strings.
public methodstatic (Shared in Visual Basic)GetDirectoryName

Supported by the .NET Compact Framework.

Returns the directory information for the specified path string.
public methodstatic (Shared in Visual Basic)GetExtension

Supported by the .NET Compact Framework.

Returns the extension of the specified path string.
public methodstatic (Shared in Visual Basic)GetFileName

Supported by the .NET Compact Framework.

Returns the file name and extension of the specified path string.
public methodstatic (Shared in Visual Basic)GetFileNameWithoutExtension

Supported by the .NET Compact Framework.

Returns the file name of the specified path string without the extension.
public methodstatic (Shared in Visual Basic)GetFullPath

Supported by the .NET Compact Framework.

Returns the absolute path for the specified path string.
public methodstatic (Shared in Visual Basic)GetPathRoot

Supported by the .NET Compact Framework.

Gets the root directory information of the specified path.
public methodstatic (Shared in Visual Basic)GetTempFileName

Supported by the .NET Compact Framework.

Returns a uniquely named zero-byte temporary file on disk and returns the full path to that file.
public methodstatic (Shared in Visual Basic)GetTempPath

Supported by the .NET Compact Framework.

Returns the path of the current system’s temporary folder.
public methodstatic (Shared in Visual Basic)HasExtension

Supported by the .NET Compact Framework.

Determines whether a path includes a file name extension.
public methodstatic (Shared in Visual Basic)IsPathRooted Gets a value indicating whether the specified path string contains absolute or relative path information.