Export (0) Print
Expand All

Path.GetFullPath Method

Returns the absolute path for the specified path string.

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

public static string GetFullPath (
	string path
)
public static String GetFullPath (
	String path
)
public static function GetFullPath (
	path : String
) : String

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".

Exception typeCondition

ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars, or contains a wildcard character.

-or-

The system could not retrieve the absolute path.

SecurityException

The caller does not have the required permissions.

ArgumentNullException

path is a null reference (Nothing in Visual Basic).

NotSupportedException

path contains a colon (":").

PathTooLongException

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.

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

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 specify a file name only 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.

If a path contains no significant characters it is invalid unless it contains one or more "." characters followed by any number of spaces, then it will be parsed as either "." or "..".

For an example of using this method, 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.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Retrieve a file extension.

GetExtension

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

Sort files in a directory by size.

GetFileSystemInfos

Determine if a directory exists.

Exists

Determine if a file exists.

Exists

The following code example demonstrates the GetFullPath method on a Windows-based desktop platform.

string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

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

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

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

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'

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);

var fileName : String = "myfile.ext";
var path : String = "\\mydir\\";
var fullPath : String;

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

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft