System.IO.Path Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.
C# Syntax:
public sealed class Path
Remarks
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 Path.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 Path.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 of the following are acceptable paths:

Because all these operations are performed on strings, it is impossible to verify that the results are valid in all scenarios. For example, the Path.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.

See also:
System.IO Namespace

System.IO.Path Member List:

Public Fields
AltDirectorySeparatorChar Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.
DirectorySeparatorChar Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.
InvalidPathChars Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.
PathSeparator A platform-specific separator character used to separate path strings in environment variables.
VolumeSeparatorChar Provides a platform-specific volume separator character.
Public Methods
ChangeExtension Changes the extension of a path string.
Combine Combines two path strings.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetDirectoryName Returns the directory information for the specified path string.
GetExtension Returns the extension of the specified path string.
GetFileName Returns the file name and extension of the specified path string.
GetFileNameWithoutExtension Returns the file name of the specified path string without the extension.
GetFullPath Returns the absolute path for the specified path string.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetPathRoot Gets the root directory information of the specified path.
GetTempFileName Returns a unique temporary file name and creates a zero-byte file by that name on disk.
GetTempPath Returns the path of the current system's temporary folder.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
HasExtension Determines whether a path includes a file name extension.
IsPathRooted Gets a value indicating whether the specified path string contains absolute or relative path information.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IO.Path Member Details

Field: AltDirectorySeparatorChar
Summary
Provides a platform-specific alternate character used to separate directory levels in a path string that reflects a hierarchical file system organization.
C# Syntax:
public static readonly char AltDirectorySeparatorChar;
Remarks
The character stored in this field cannot be in Path.InvalidPathChars. This field can be set to the same value as Path.DirectorySeparatorChar.AltDirectorySeparatorChar and DirectorySeparatorChar are both valid for separating directory levels in a path string.

The value of this field is a backslash ('\') on Unix, and a slash ('/') on Windows and Macintosh operating systems.

Example
The following example code demonstrates the use of the AltDirectorySeparatorChar field.
        Console.WriteLine("Path.AltDirectorySeparatorChar={0}", 
                          Path.AltDirectorySeparatorChar);
        Console.WriteLine("Path.DirectorySeparatorChar={0}", 
                          Path.DirectorySeparatorChar);
        Console.WriteLine("Path.PathSeparator={0}", 
                          Path.PathSeparator);
        Console.WriteLine("Path.VolumeSeparatorChar={0}", 
                          Path.VolumeSeparatorChar);
        
        Console.Write("Path.InvalidPathChars=");
        foreach (char c in Path.InvalidPathChars)
            Console.Write(c);
        Console.WriteLine();

    

Return to top


Field: DirectorySeparatorChar
Summary
Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.
C# Syntax:
public static readonly char DirectorySeparatorChar;
Remarks
The character stored in this field cannot be in Path.InvalidPathChars. Path.AltDirectorySeparatorChar and DirectorySeparatorChar are both valid for separating directory levels in a path string.

The value of this field is a backslash ('\') on Unix, and a slash ('/') on Windows and Macintosh operating systems.

Example
The following example code demonstrates the use of the DirectorySeparatorChar field.
        Console.WriteLine("Path.AltDirectorySeparatorChar={0}", 
                          Path.AltDirectorySeparatorChar);
        Console.WriteLine("Path.DirectorySeparatorChar={0}", 
                          Path.DirectorySeparatorChar);
        Console.WriteLine("Path.PathSeparator={0}", 
                          Path.PathSeparator);
        Console.WriteLine("Path.VolumeSeparatorChar={0}", 
                          Path.VolumeSeparatorChar);
        
        Console.Write("Path.InvalidPathChars=");
        foreach (char c in Path.InvalidPathChars)
            Console.Write(c);
        Console.WriteLine();

    

Return to top


Field: InvalidPathChars
Summary
Provides a platform-specific array of characters that cannot be specified in path string arguments passed to members of the Path class.
C# Syntax:
public static readonly char[] InvalidPathChars;
Remarks
This array is not guaranteed to contain the complete set of characters that are invalid in file and directory names. The full set of invalid characters can vary by file system. 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.
Example
The following example code demonstrates the use of the InvalidPathChars property.
        Console.WriteLine("Path.AltDirectorySeparatorChar={0}", 
                          Path.AltDirectorySeparatorChar);
        Console.WriteLine("Path.DirectorySeparatorChar={0}", 
                          Path.DirectorySeparatorChar);
        Console.WriteLine("Path.PathSeparator={0}", 
                          Path.PathSeparator);
        Console.WriteLine("Path.VolumeSeparatorChar={0}", 
                          Path.VolumeSeparatorChar);
        
        Console.Write("Path.InvalidPathChars=");
        foreach (char c in Path.InvalidPathChars)
            Console.Write(c);
        Console.WriteLine();

    

Return to top


Field: PathSeparator
Summary
A platform-specific separator character used to separate path strings in environment variables.
C# Syntax:
public static readonly char PathSeparator;
Remarks
On Windows-based desktop platforms, the value of this field is the semicolon (;) by default, but might vary on other platforms.
Example
The following example code demonstrates the use of the PathSeparator field.
        Console.WriteLine("Path.AltDirectorySeparatorChar={0}", 
                          Path.AltDirectorySeparatorChar);
        Console.WriteLine("Path.DirectorySeparatorChar={0}", 
                          Path.DirectorySeparatorChar);
        Console.WriteLine("Path.PathSeparator={0}", 
                          Path.PathSeparator);
        Console.WriteLine("Path.VolumeSeparatorChar={0}", 
                          Path.VolumeSeparatorChar);
        
        Console.Write("Path.InvalidPathChars=");
        foreach (char c in Path.InvalidPathChars)
            Console.Write(c);
        Console.WriteLine();

    

Return to top


Field: VolumeSeparatorChar
Summary
Provides a platform-specific volume separator character.
C# Syntax:
public static readonly char VolumeSeparatorChar;
Remarks
The value of this field is a colon (':') on Windows and Macintosh, and a slash ('/') on Unix operating systems. This is most useful for parsing paths such as "c:\windows" or "MacVolume:System Folder".
Example
The following example code demonstrates the use of the VolumeSeparatorChar field.
        Console.WriteLine("Path.AltDirectorySeparatorChar={0}", 
                          Path.AltDirectorySeparatorChar);
        Console.WriteLine("Path.DirectorySeparatorChar={0}", 
                          Path.DirectorySeparatorChar);
        Console.WriteLine("Path.PathSeparator={0}", 
                          Path.PathSeparator);
        Console.WriteLine("Path.VolumeSeparatorChar={0}", 
                          Path.VolumeSeparatorChar);
        
        Console.Write("Path.InvalidPathChars=");
        foreach (char c in Path.InvalidPathChars)
            Console.Write(c);
        Console.WriteLine();

    

Return to top


Method: ChangeExtension(
   string path,
   string extension
)
Summary
Changes the extension of a path string.
C# Syntax:
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 Path.InvalidPathChars.

extension

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

Return Value:
A string containing 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
Exception Type Condition
ArgumentException The path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
If neither path nor extension contains a period (.), ChangeExtension adds the period.

The extension parameter can contain multiple periods and any valid path characters, and can be any length. If extension is null, the returned string contains the contents of path with the last period and all characters following it removed.

If extension is an empty string, the returned path string contains the contents of path with any characters following the last period removed.

If path does not have an extension and extension is not null, the returned string contains path followed by extension.

If extension is not null and does not contain a leading period, the period is added.

If path contains a multiple extension separated by multiple periods, the returned string contains the contents of path with the last period and all characters following it replaced by extension. For example, if path is "\Dir1\examples\pathtests.csx.txt" and extension is "cs", the modified path is "\Dir1\examples\pathtests.csx.cs".

It is not possible to verify that the returned results are valid in all scenarios. For example, if path is empty, extension is appended.

Example
The following example code demonstrates the use of the ChangeExtension method.
        string goodFileName = @"C:\mydir\myfile.com.extension";
        string badFileName = @"C:\mydir\";
        string result;

        result = Path.ChangeExtension(goodFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
                          goodFileName, result); 

        result = Path.ChangeExtension(goodFileName, "");
        Console.WriteLine("ChangeExtension({0}, '') returns '{1}'", 
                          goodFileName, result); 

        result = Path.ChangeExtension(badFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", 
                          badFileName, result); 

    

Return to top


Method: Combine(
   string path1,
   string path2
)
Summary
Combines two path strings.
C# Syntax:
public static string Combine(
   string path1,
   string path2
);
Parameters:

path1

The first path.

path2

The second path, which cannot be a volume letter or a Universal Naming Convention (UNC) name.

Return Value:
A string containing 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
Exception Type Condition
ArgumentException IsPathRooted is true.

-or-

path1 is not empty, and path2 is a volume letter, a UNC share name, or begins with a path separator character.

-or-

path1 or path2 contain one or more of the invalid characters defined in Path.InvalidPathChars.

ArgumentNullException path1 or path2 is null.
Remarks
If path1 does not end with a valid separator character as defined in Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation.

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, the root is returned.

If path2 has no leading white space and begins with a Universal Naming Convention (UNC) name or volume letter, this will not throw an exception. Because the parameters are not parsed if they have white space, if path2 is " c:\\ ", this will be appended successfully and will not be verified. Strings such as "c :\\" and " \\ hello.txt" will also be accepted.

Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

Example
The following example demonstrates using the Combine method on a Windows-based desktop platform.
using System;
using System.IO;

public class ChangeExtensionTest {

    public static void Main() {

        string path1 = "c:\\temp";
        string path2 = "subdir\\file.txt";
        string path3 = "c:\\temp.txt";
        string path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
        string path5 = "";
        string path6 = null;

        CombinePaths(path1, path2);
        CombinePaths(path1, path3);
        CombinePaths(path3, path2);
        CombinePaths(path4, path2);
        CombinePaths(path5, path2);
        CombinePaths(path6, path2);
    }

    private static void CombinePaths(string p1, string p2) {

        try {
            string combination = Path.Combine(p1, p2);

            Console.WriteLine("When you combine '{0}' and '{1}', the result is: {2}'{3}'",
                        p1, p2, Environment.NewLine, combination);
        } catch (Exception e) {
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}",
                        p1, p2, Environment.NewLine, e.Message);
        }

        Console.WriteLine();
    }
}

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Path();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetDirectoryName(
   string path
)
Summary
Returns the directory information for the specified path string.
C# Syntax:
public static string GetDirectoryName(
   string path
);
Parameters:

path

The path of a file or directory.

Return Value:
A String containing directory information for path, or null if path denotes a root directory, is the empty string (""), or is null. Returns String.Empty if path does not contain directory information.
Exceptions
Exception Type Condition
ArgumentException path contains invalid characters, is empty, or contains only white spaces.
Remarks
The string returned by this method consists of all characters between the first and last Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar character in path. The first separator character is included, but the last separator character is not included in the returned string.
Example
The following example demonstrates using the GetDirectoryName method on a Windows-based desktop platform.
        string fileName = @"C:\mydir\myfile.ext";
        string path = @"C:\mydir\";
        string rootPath = @"C:\";
        string directoryName;
    
        directoryName = Path.GetDirectoryName(fileName);
        Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", 
                          fileName, directoryName);

        directoryName = Path.GetDirectoryName(path);
        Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", 
                          path, directoryName);

        directoryName = Path.GetDirectoryName(rootPath);
        Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", 
                          rootPath, directoryName);

    

Return to top


Method: GetExtension(
   string path
)
Summary
Returns the extension of the specified path string.
C# Syntax:
public static string GetExtension(
   string path
);
Parameters:

path

The path string from which to get the extension.

Return Value:
A String containing the extension of the specified path (including the "."), null, or String.Empty. If path is null, GetExtension returns null. If path does not have extension information, GetExtension returns Empty.
Exceptions
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
The extension of path is obtained by searching path for a period (.), starting with the last character in path and continuing toward the start of path. If a period is found before a Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, String.Empty is returned.
Example
The following example demonstrates using the GetExtension method on a Windows-based desktop platform.
        string fileName = @"C:\mydir.old\myfile.ext";
        string path = @"C:\mydir.old\";
        string extension;

        extension = Path.GetExtension(fileName);
        Console.WriteLine("GetExtension('{0}') returns '{1}'", 
                          fileName, extension);

        extension = Path.GetExtension(path);
        Console.WriteLine("GetExtension('{0}') returns '{1}'", 
                          path, extension);

    

Return to top


Method: GetFileName(
   string path
)
Summary
Returns the file name and extension of the specified path string.
C# Syntax:
public static string GetFileName(
   string path
);
Parameters:

path

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

Return Value:
A String consisting of the characters after the last directory character in path. If the last character of path is a directory separator character, this method returns String.Empty. If path is null, this method returns null.
Exceptions
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
This method effectively removes the last element of the given file path, returning a string consisting of all characters up to but not including the last separator character in the file path. If you pass a string ending in a path separator character, it will be passed one level lower. The returned value is null if the file path is null or if the file path denotes a root (such as "\", "C:", or "\\server\share").

The separator characters used to determine the start of the file name are Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar.

Example
The following example demonstrates the behavior of the GetFileName method on a Windows-based desktop platform.
        string fileName = @"C:\mydir\myfile.ext";
        string path = @"C:\mydir\";
        string result;

        result = Path.GetFileName(fileName);
        Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                          fileName, result);

        result = Path.GetFileName(path);
        Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                          path, result);

    

Return to top


Method: GetFileNameWithoutExtension(
   string path
)
Summary
Returns the file name of the specified path string without the extension.
C# Syntax:
public static string GetFileNameWithoutExtension(
   string path
);
Parameters:

path

The path of the file.

Return Value:
A String containing the string returned by Path.GetFileName, minus the last period (.) and all characters following it.
Exceptions
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
This method does not verify that the path or file name exists.
Example
The following example code demonstrates the use of the GetFileNameWithoutExtension method.
        string fileName = @"C:\mydir\myfile.ext";
        string path = @"C:\mydir\";
        string result;

        result = Path.GetFileNameWithoutExtension(fileName);
        Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
                          fileName, result);

        result = Path.GetFileName(path);
        Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
                          path, result);

    

Return to top


Method: GetFullPath(
   string path
)
Summary
Returns the absolute path for the specified path string.
C# Syntax:
public static string GetFullPath(
   string path
);
Parameters:

path

The file or directory for which to obtain absolute path information

Return Value:
A string containing the fully qualified location of path, such as "C:\MyFile.txt". If path is null, GetFullPath returns null.
Exceptions
Exception Type Condition
ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars.

-or-

The system could not retrieve the absolute path.

SecurityException The caller does not have the required permissions.
ArgumentNullException path is null.
PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
Remarks
The absolute path includes all information required to locate a file or directory on a system.

The file or directory specified by path is not required to exist. For example, if c:\temp\newdir is the current directory, calling GetFullPath on a file name such as test.txt returns c:\temp\newdir\test.txt. The file need not exist.

However, if path does exist, the caller must have permission to obtain path information for path. Note that unlike most members of the Path class, this method accesses the file system.

This method uses current directory and current volume information to fully qualify path. If you only specify a file name in path, GetFullPath returns the fully qualified path of the current directory.

If you pass in a short file name, it is not expanded to a long file name.

Example
The following example demonstrates the GetFullPath method on a Windows-based desktop platform.
        string fileName = "myfile.ext";
        string path = @"\mydir\";
        string fullPath;

        fullPath = Path.GetFullPath(path);
        Console.WriteLine("GetFullPath('{0}') returns '{1}'", 
                          path, fullPath);
        
        fullPath = Path.GetFullPath(fileName);
        Console.WriteLine("GetFullPath('{0}') returns '{1}'", 
                          fileName, fullPath);

    
.NET Framework Security:
FileIOPermission for access to the path. Associated enumeration: FileIOPermissionAccess.PathDiscovery

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetPathRoot(
   string path
)
Summary
Gets the root directory information of the specified path.
C# Syntax:
public static string GetPathRoot(
   string path
);
Parameters:

path

The path from which to obtain root directory information.

Return Value:
A string containing the root directory of path, such as "C:\", or null if path is null or does not contain root directory information.
Exceptions
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars,

-or-

String.Empty was passed to path.

Remarks
This method does not verify that the path or file name exists.

Possible patterns for the string returned by this method are as follows:

Example
The following example code demonstrates the use of the GetPathRoot method.
        string path = @"\mydir\";
        string fileName = "myfile.ext";
        string fullPath = @"C:\mydir\myfile.ext";
        string pathRoot;

        pathRoot = Path.GetPathRoot(path);
        Console.WriteLine("GetPathRoot('{0}') returns '{1}'", 
                          path, pathRoot);
        
        pathRoot = Path.GetPathRoot(fileName);
        Console.WriteLine("GetPathRoot('{0}') returns '{1}'", 
                          fileName, pathRoot);

        pathRoot = Path.GetPathRoot(fullPath);
        Console.WriteLine("GetPathRoot('{0}') returns '{1}'", 
                          fullPath, pathRoot);

    

Return to top


Method: GetTempFileName()
Summary
Returns a unique temporary file name and creates a zero-byte file by that name on disk.
C# Syntax:
public static string GetTempFileName();
Return Value:
A String containing the name of the temporary file.
Remarks
The following example code demonstrates the use of the GetTempFileName method.
        string fileName = Path.GetTempFileName();

        FileInfo  fileInfo = new FileInfo(fileName);
        Console.WriteLine("File '{0}' created of size {1} bytes", 
                          fileName, fileInfo.Length);
        
        // Write some text to the file.
        FileStream f = new FileStream(fileName, FileMode.Open);
        StreamWriter s = new StreamWriter(f);
        s.WriteLine("Output to the file");
        s.Close();
        f.Close();

        fileInfo.Refresh();                                            
        Console.WriteLine("File '{0}' now has size {1} bytes", 
                          fileName, fileInfo.Length);

    

Return to top


Method: GetTempPath()
Summary
Returns the path of the current system's temporary folder.
C# Syntax:
public static string GetTempPath();
Return Value:
A String containing the path information of a temporary directory.
Exceptions
Exception Type Condition
SecurityException The caller does not have the required permissions.
Remarks
The following example code demonstrates the use of the GetTempPath method.
        string tempPath = Path.GetTempPath();
        Console.WriteLine("Temporary path is '{0}'", tempPath);

        DirectoryInfo tempDir = new DirectoryInfo(tempPath);
        Console.WriteLine("{0} contains {1} files", tempPath, 
                          tempDir.GetFiles().Length);

    
.NET Framework Security:
EnvironmentPermission for unrestricted access to environment variables. Associated enumeration: PermissionState.Unrestricted

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: HasExtension(
   string path
)
Summary
Determines whether a path includes a file name extension.
C# Syntax:
public static bool HasExtension(
   string path
);
Parameters:

path

The path to search for an extension.

Return Value:
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
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
Starting from the end of path, this method searches for a period (.) followed by at least one character. If this pattern is found before a Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar, or Path.VolumeSeparatorChar character is encountered, this method returns true.
Example
The following example code demonstrates the use of the HasExtension method.
        string fileName1 = "myfile.ext";
        string fileName2 = @"mydir\myfile";
        string path = @"C:\mydir.ext\";
        bool result;

        result = Path.HasExtension(fileName1);
        Console.WriteLine("HasExtension('{0}') returns {1}", 
                          fileName1, result);

        result = Path.HasExtension(fileName2);
        Console.WriteLine("HasExtension('{0}') returns {1}", 
                          fileName2, result);
        
        result = Path.HasExtension(path);
        Console.WriteLine("HasExtension('{0}') returns {1}", 
                          path, result);

    

Return to top


Method: IsPathRooted(
   string path
)
Summary
Gets a value indicating whether the specified path string contains absolute or relative path information.
C# Syntax:
public static bool IsPathRooted(
   string path
);
Parameters:

path

The path to test.

Return Value:
true if path contains an absolute path; otherwise, false.
Exceptions
Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in Path.InvalidPathChars.
Remarks
This method does not verify that the path or file name exists.

IsPathRooted returns true for path strings such as "\\MyDir\\MyFile.txt" and "C:\\MyDir". It returns false for path strings such as "MyDir".

Example
        string fileName = @"C:\mydir\myfile.ext";
        string UncPath = @"\\myPc\mydir\myfile";
        string relativePath = @"mydir\sudir\";
        bool result;

        result = Path.IsPathRooted(fileName);
        Console.WriteLine("IsPathRooted('{0}') returns {1}", 
                          fileName, result);

        result = Path.IsPathRooted(UncPath);
        Console.WriteLine("IsPathRooted('{0}') returns {1}", 
                          UncPath, result);
        
        result = Path.IsPathRooted(relativePath);
        Console.WriteLine("IsPathRooted('{0}') returns {1}", 
                          relativePath, result);

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.