Path Class

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

Syntax

Declaration

public static class Path

Inheritance Hierarchy

Inherited Members

System.Object

, , , , , ,

Fields summary

Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.

Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.

A platform-specific separator character used to separate path strings in environment variables.

Provides a platform-specific volume separator character.

Methods summary

Changes the extension of a path string.

Combines two strings into a path.

Combines three strings into a path.

Combines an array of strings into a path.

Returns the directory information for the specified path string.

Returns the extension of the specified path string.

Returns the file name and extension of the specified path string.

Returns the file name of the specified path string without the extension.

Returns the absolute path for the specified path string.

Gets an array containing the characters that are not allowed in file names.

Gets an array containing the characters that are not allowed in path names.

Gets the root directory information of the specified path.

Returns a random folder name or file name.

Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

Returns the path of the current user's temporary folder.

Determines whether a path includes a file name extension.

Gets a value indicating whether the specified path string contains a root.

Fields

  • AltDirectorySeparatorChar

    Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.

    public static readonly char AltDirectorySeparatorChar
  • DirectorySeparatorChar

    Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.

    public static readonly char DirectorySeparatorChar
  • PathSeparator

    A platform-specific separator character used to separate path strings in environment variables.

    public static readonly char PathSeparator
  • VolumeSeparatorChar

    Provides a platform-specific volume separator character.

    public static readonly char VolumeSeparatorChar

Methods

  • ChangeExtension(String, String)

    Changes the extension of a path string.

    public static string ChangeExtension(string path, string extension)

    Parameters

    • path

      The path information to modify. The path cannot contain any of the characters defined in GetInvalidPathChars().

    • extension

      The new extension (with or without a leading period). Specify null to remove an existing extension from path.

    Returns

    • The modified path information.

      On Windows-based desktop platforms, if path is null or an empty string (""), the path information is returned unmodified. If extension is null, the returned string contains the specified path with its extension removed. If path has no extension, and extension is not null, the returned path string contains extension appended to the end of path.

    Exceptions

  • Combine(String, String)

    Combines two strings into a path.

    public static string Combine(string path1, string path2)

    Parameters

    • path1

      The first path to combine.

    • path2

      The second path to combine.

    Returns

    • The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.

    Exceptions

  • Combine(String, String, String)

    Combines three strings into a path.

    public static string Combine(string path1, string path2, string path3)

    Parameters

    • path1

      The first path to combine.

    • path2

      The second path to combine.

    • path3

      The third path to combine.

    Returns

    Exceptions

  • Combine(String[])

    Combines an array of strings into a path.

    public static string Combine(params string[] paths)

    Parameters

    • paths

      An array of parts of the path.

    Returns

    Exceptions

  • GetDirectoryName(String)

    Returns the directory information for the specified path string.

    public static string GetDirectoryName(string path)

    Parameters

    • path

      The path of a file or directory.

    Returns

    • Directory information for path, or null if path denotes a root directory or is null. Returns Empty if path does not contain directory information.

    Exceptions

  • GetExtension(String)

    Returns the extension of the specified path string.

    [Pure]
    public static string GetExtension(string path)

    Parameters

    • path

      The path string from which to get the extension.

    Returns

    Exceptions

  • GetFileName(String)

    Returns the file name and extension of the specified path string.

    [Pure]
    public static string GetFileName(string path)

    Parameters

    • path

      The path string from which to obtain the file name and extension.

    Returns

    • The characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns Empty. If path is null, this method returns null.

    Exceptions

  • GetFileNameWithoutExtension(String)

    Returns the file name of the specified path string without the extension.

    [Pure]
    public static string GetFileNameWithoutExtension(string path)

    Parameters

    • path

      The path of the file.

    Returns

    Exceptions

  • GetFullPath(String)

    Returns the absolute path for the specified path string.

    public static string GetFullPath(string path)

    Parameters

    • path

      The file or directory for which to obtain absolute path information.

    Returns

    • The fully qualified location of path, such as "C:\MyFile.txt".

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more of the invalid characters defined in GetInvalidPathChars().

      -or-

      The system could not retrieve the absolute path.

    • The caller does not have the required permissions.

    • path is null.

    • path contains a colon (":") that is not part of a volume identifier (for example, "c:\").

    • The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

  • GetInvalidFileNameChars()

    Gets an array containing the characters that are not allowed in file names.

    public static char[] GetInvalidFileNameChars()

    Returns

    • An array containing the characters that are not allowed in file names.

  • GetInvalidPathChars()

    Gets an array containing the characters that are not allowed in path names.

    public static char[] GetInvalidPathChars()

    Returns

    • An array containing the characters that are not allowed in path names.

  • GetPathRoot(String)

    Gets the root directory information of the specified path.

    public static string GetPathRoot(string path)

    Parameters

    • path

      The path from which to obtain root directory information.

    Returns

    • The root directory of path, such as "C:\", or null if path is null, or an empty string if path does not contain root directory information.

    Exceptions

  • GetRandomFileName()

    Returns a random folder name or file name.

    public static string GetRandomFileName()

    Returns

    • A random folder name or file name.

  • GetTempFileName()

    Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

    public static string GetTempFileName()

    Returns

    • The full path of the temporary file.

    Exceptions

    • An I/O error occurs, such as no unique temporary file name is available.

      - or -

      This method was unable to create a temporary file.

  • GetTempPath()

    Returns the path of the current user's temporary folder.

    public static string GetTempPath()

    Returns

    • The path to the temporary folder, ending with a backslash.

    Exceptions

  • HasExtension(String)

    Determines whether a path includes a file name extension.

    [Pure]
    public static bool HasExtension(string path)

    Parameters

    • path

      The path to search for an extension.

    Returns

    • true if the characters that follow the last directory separator (\ or /) or volume separator (:) in the path include a period (.) followed by one or more characters; otherwise, false.

    Exceptions

  • IsPathRooted(String)

    Gets a value indicating whether the specified path string contains a root.

    public static bool IsPathRooted(string path)

    Parameters

    • path

      The path to test.

    Returns

    • true if path contains a root; otherwise, false.

    Exceptions

Details

Namespace

System.IO

Assembly

System.Runtime.Extensions.dll