Working with .NET files via the Path class

The .NET Path class, which allows you to work with file path values, uses string values to perform all of its operations. Learn how the class uses various properties and methods to perform operations.

Developers often need to know certain aspects of a file such as the name, full path, drive, and file extension. The .NET Framework provides easy access to these details via the Path class contained in the System.IO namespace. This article examines the various pieces of information available in the Path class with a focus on .NET Framework 2.0.

The Path class

The Path class allows you to work with file path values. A key aspect of the class is the fact that it uses string values to perform all of its operations. This may not seem like a big deal, but the string may be an actual file name or a random string. You should utilize other classes, such as the File class, to confirm the existence of a file. The class provides various properties and methods to perform operations.

Many of the methods contained in the Path class accept a path as a parameter. A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk. The exact format of a path is determined by the current platform. A path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name.

Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

The following list provides a sampling of the methods available in the Path class:

  • ChangeExtension: Allows you to change the file extension of a path string.
  • Combines: Allows you to combine two path strings into one.
  • GetDirectoryName: Returns the directory information included in a path string.
  • GetExtension: Returns the extension included in a path string.
  • GetFileName: Returns the file name and extension of a path string.
  • GetFileNameWithoutExtension: Returns the file name without the extension for a path string.
  • GetFullPath: Returns the absolute path for a path string.
  • GetInvalidFileNameChars: Returns a character array containing the characters not allowed in file names.
  • GetPathRoot: Returns the root directory information for a path string.
  • GetRandomFileName: Returns a random file or folder name.
  • GetTempFileName: A uniquely named, zero-byte temporary file is created with the full path returned.
  • GetTempPath: The path to the system's temporary directory is returned.
  • HasExtension: Determines if a path string contains an extension (true) or not (false).
  • IsPathRooted: Gets a value indicating whether the specified path string contains absolute or relative path information.

The best way to understand how to use these methods is with an example. The C# sample in Listing A provides a peek at using the Path class. Listing B contains the equivalent VB.NET code.

This simple application displays various characteristics of the path string used. (Note: A character array is populated with the results of a call to the GetInvalidFileNameChars method, but the contents of the character array are not displayed due to problems with displaying such characters on the Web.) The latter portion of the application determines if the file path exists via the Exists method of the File class. Finally, the Combines method concatenates two values into one path string. The output of the application (on my system) follows:

Directory name: c:\
File extension: .txt
Filename: techrepublic.txt
Filename w/o ext: techrepublic
Full path: c:\techrepublic.txt
Root: c:\
Random file name: km54krxb.s1g
Temp file name: C:\Documents and Settings\tpatton\Local Settings
File does exist.
The file c:\test.exe does not exist.

The Path class provides properties as well. These properties allow you to manipulate various elements of path strings and how the system handles characters within it. The follow list provides an overview of these properties:

  • AltDirectorySeparatorChar: Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization. The value of this property is a backslash (\) on UNIX and a slash (/) on Windows and Macintosh operating systems.
  • DirectorySeparatorChar: Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization. The value of this property is a backslash (\) on UNIX, and a slash (/) on Windows and Macintosh operating systems.
  • PathSeparator: A platform-specific separator character used to separate path strings in environment variables. On Windows-based desktop platforms, the value of this field is the semicolon (;) by default, but it might vary on other platforms.
  • VolumeSeparatorChar: Provides a platform-specific volume separator character. The value of this field is a colon (:) on Windows and Macintosh, and a slash (/) on UNIX operating systems.

A simple code snippet allows you to view these properties on your system as the C# example in Listing C illustrates. Listing D contains the equivalent VB.NET code.

The following output is generated on my system:

AltDirectorySeparatorChar - /
DirectorySeparatorChar - \
PathSeparator - ;
VolumeSeparatorChar - :

The path to success

The .NET Path class provides a simple and straightforward way to access information about a file and its path. This includes the name of the file, directory information, and the details of the file extension assigned to the file. This allows you to quickly extract the necessary data and move forward. Combine it with the remaining classes of the System.IO namespace, and you have a powerful set of tools for working with files.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox