Export (0) Print
Expand All
1 out of 8 rated this helpful - Rate this topic

Path Class

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

For a list of all members of this type, see Path Members.

System.Object
   System.IO.Path

[Visual Basic]
NotInheritable Public Class Path
[C#]
public sealed class Path
[C++]
public __gc __sealed class Path
[JScript]
public class Path

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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 if the string contains characters that are not valid in path strings, as defined in InvalidPathChars. 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.

Note   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 an example of using this class, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension
Determine if a directory exists. Exists
Determine if a file exists. Exists

Example

[Visual Basic, C#, C++] The following example demonstrates some of the main members of the Path class.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path1 As String = "c:\temp\MyTest.txt"
        Dim path2 As String = "c:\temp\MyTest"
        Dim path3 As String = "temp"

        If Path.HasExtension(path1) Then
            Console.WriteLine("{0} has an extension.", path1)
        End If

        If Path.HasExtension(path2) = False Then
            Console.WriteLine("{0} has no extension.", path2)
        End If

        If Path.IsPathRooted(path3) = False Then
            Console.WriteLine("The string {0} contains no root information.", path3)
        End If

        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(Environment.NewLine & _
          "The set of invalid characters in a path is:")
        Console.WriteLine("(Note that the wild card characters '*' and '?' are not invalid.):")

        Dim c As Char
        For Each c In Path.InvalidPathChars
            Console.WriteLine(c)
        Next
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    
    public static void 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.):");
        foreach (char c in Path.InvalidPathChars) 
        {
            Console.WriteLine(c);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path1 = S"c:\\temp\\MyTest.txt";
    String* path2 = S"c:\\temp\\MyTest";
    String* path3 = S"temp";

    if (Path::HasExtension(path1)) {
        Console::WriteLine(S"{0} has an extension.", path1);
    }

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

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

    Console::WriteLine(S"The full path of {0} is {1}.", path3, Path::GetFullPath(path3));
    Console::WriteLine(S"{0} is the location for temporary files.", Path::GetTempPath());
    Console::WriteLine(S"{0} is a file available for use.", Path::GetTempFileName());

    Console::WriteLine(S"\r\nThe set of invalid characters in a path is:");
    Console::WriteLine(S"(Note that the wildcard characters '*' and '?' are not invalid.):");
    Collections::IEnumerator* myEnum = Path::InvalidPathChars->GetEnumerator();
    while (myEnum->MoveNext()) {
        Char c = *__try_cast<__box Char*>(myEnum->Current);
        Console::WriteLine(c);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.