Path Class

Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.

To browse the .NET Framework source code for this type, see the Reference Source.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
public ref class Path abstract sealed

The Path type exposes the following members.

  NameDescription
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsChangeExtensionChanges the extension of a path string.
Public methodStatic memberSupported in .NET for Windows Store appsCombine(array<String>)Combines an array of strings into a path.
Public methodStatic memberSupported by the XNA FrameworkCombine(String, String)Combines two strings into a path.
Public methodStatic memberCombine(String, String, String)Combines three strings into a path.
Public methodStatic memberCombine(String, String, String, String)Combines four strings into a path.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetDirectoryNameReturns the directory information for the specified path string.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetExtensionReturns the extension of the specified path string.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetFileNameReturns the file name and extension of the specified path string.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetFileNameWithoutExtensionReturns the file name of the specified path string without the extension.
Public methodStatic memberSupported by the XNA FrameworkGetFullPathReturns the absolute path for the specified path string.
Public methodStatic memberSupported in .NET for Windows Store appsGetInvalidFileNameCharsGets an array containing the characters that are not allowed in file names.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetInvalidPathCharsGets an array containing the characters that are not allowed in path names.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetPathRootGets the root directory information of the specified path.
Public methodStatic memberSupported in .NET for Windows Store appsGetRandomFileNameReturns a random folder name or file name.
Public methodStatic memberGetTempFileNameCreates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.
Public methodStatic memberGetTempPathReturns the path of the current user's temporary folder.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsHasExtensionDetermines whether a path includes a file name extension.
Public methodStatic memberSupported by the XNA FrameworkSupported in .NET for Windows Store appsIsPathRootedGets a value indicating whether the specified path string contains a root.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkAltDirectorySeparatorCharProvides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.
Public fieldStatic memberSupported by the XNA FrameworkDirectorySeparatorCharProvides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.
Public fieldStatic memberSupported by the XNA FrameworkInvalidPathChars Obsolete. Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.
Public fieldStatic memberSupported by the XNA FrameworkPathSeparatorA platform-specific separator character used to separate path strings in environment variables.
Public fieldStatic memberSupported by the XNA FrameworkVolumeSeparatorCharProvides a platform-specific volume separator character.
Top

NoteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".

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; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform. For example, on some systems, a path can start with a drive or volume letter, while this element is not present in other systems. On some systems, file paths can contain extensions, which indicate the type of information stored in the file. The format of a file name extension is platform-dependent; for example, some systems limit extensions to three characters, and others do not. The current platform also determines the set of characters used to separate the elements of a path, and the set of characters that cannot be used when specifying paths. Because of these differences, the fields of the Path class as well as the exact behavior of some members of the Path class are platform-dependent.

A path can contain absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. Relative paths specify a partial location: the current location is used as the starting point when locating a file specified with a relative path. To determine the current directory, call Directory::GetCurrentDirectory.

Most members of the Path class do not interact with the file system and do not verify the existence of the file specified by a path string. Path class members that modify a path string, such as ChangeExtension, have no effect on names of files in the file system. Path members do, however, validate the contents of a specified path string, and throw an ArgumentException exception if the string contains characters that are not valid in path strings, as defined in the characters returned from the GetInvalidPathChars method. For example, on Windows-based desktop platforms, invalid path characters might include quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0), and Unicode characters 16 through 18 and 20 through 25.

The members of the Path class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name.

All members of the Path class are static and can therefore be called without having an instance of a path.

NoteNote

In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

In members that accept a path, the 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. For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

Because all these operations are performed on strings, it is impossible to verify that the results are valid in all scenarios. For example, the GetExtension method parses a string that you pass to it and returns the extension from that string. However, this does not mean that a file with that extension exists on the disk.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example demonstrates some of the main members of the Path class.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path1 = "c:\\temp\\MyTest.txt";
   String^ path2 = "c:\\temp\\MyTest";
   String^ path3 = "temp";
   if ( Path::HasExtension( path1 ) )
   {
      Console::WriteLine( "{0} has an extension.", path1 );
   }

   if (  !Path::HasExtension( path2 ) )
   {
      Console::WriteLine( "{0} has no extension.", path2 );
   }

   if (  !Path::IsPathRooted( path3 ) )
   {
      Console::WriteLine( "The string {0} contains no root information.", path3 );
   }

   Console::WriteLine( "The full path of {0} is {1}.", path3, Path::GetFullPath( path3 ) );
   Console::WriteLine( "{0} is the location for temporary files.", Path::GetTempPath() );
   Console::WriteLine( "{0} is a file available for use.", Path::GetTempFileName() );
   Console::WriteLine( "\r\nThe set of invalid characters in a path is:" );
   Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" );
   Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Char c =  *safe_cast<Char^>(myEnum->Current);
      Console::WriteLine( c );
   }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft