Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.

Posted 02-10-2005 12:36 PM by Jeffrey Palermo

[Advertisement]

Comments

Noah Coad [MS] wrote re: The elusive Path class and it's "hidden" static methods - level 200
on 02-19-2005 2:01 PM
System.IO is one of my favorite namespaces since I do a lot of work with files. Path, Directory, and File are some of my most used 'utilitiy' types (I coined the term for types with mostly static members).

For example, one common task is that I have a file path I want with a different extension.
private string ChangeFileExt(string FilePath, string NewExt)
{ return Path.Combine(Path.GetDirectoryName(FilePath), Path.GetFileNameWithoutExtension(FilePath) + "." + NewExt); }

Called with:
Trace.WriteLine(ChangeFileExt(@"c:\temp\example.exe", "txt"));

One nice consistancy in the types, is that "Name" refers to just a file's name (ex: Run.exe), "Path" refers to the whole path (ex: c:\temp\example.exe), and "Directory" refers to the folder structure leading up to the path (c:\temp). I can't even count the number of times other devs I've worked with have used inconsistant member names which causes pain. They'll call a property "FileName" and have the whole path. <sigh>