Breyta

Deila með


Path.GetFullPath Method

Definition

Overloads

GetFullPath(String)

Returns the absolute path for the specified path string.

GetFullPath(String, String)

Returns an absolute path from a relative path and a fully qualified base path.

GetFullPath(String)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Returns the absolute path for the specified path string.

public:
 static System::String ^ GetFullPath(System::String ^ path);
public static string GetFullPath (string path);
static member GetFullPath : string -> string
Public Shared Function GetFullPath (path As String) As String

Parameters

path
String

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 on Windows systems, 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.

.NET Framework only: 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.

Examples

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

// 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 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'
Dim fileName As string = "myfile.ext"
Dim path1 As string = "mydir"
Dim path2 As string = "\mydir"
Dim fullPath As string

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'

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.

Important

If path is a relative path, this overload returns a fully qualified path that can be based on the current drive and current directory. The current drive and current directory can change at any time as an application executes. As a result, the path returned by this overload cannot be determined in advance. To return a deterministic path, call the GetFullPath(String, String) overload. You can also call the IsPathFullyQualified method to determine whether a path is fully qualified or relative and therefore whether a call to GetFullPath is necessary.

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

.NET Core 1.1 and later versions and .NET Framework 4.6.2 and later versions also support paths that include device names, such as "\\?\C:\".

For more information on file path formats on Windows, see File path formats on Windows systems. For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

GetFullPath(String, String)

Source:
Path.Unix.cs
Source:
Path.Unix.cs
Source:
Path.Unix.cs

Returns an absolute path from a relative path and a fully qualified base path.

public:
 static System::String ^ GetFullPath(System::String ^ path, System::String ^ basePath);
public static string GetFullPath (string path, string basePath);
static member GetFullPath : string * string -> string
Public Shared Function GetFullPath (path As String, basePath As String) As String

Parameters

path
String

A relative path to concatenate to basePath.

basePath
String

The beginning of a fully qualified path.

Returns

The absolute path.

Exceptions

path or basePath is null.

basePath is not a fully qualified path.

-or-

path or basePath contains one or more of the invalid characters defined in GetInvalidPathChars().

Examples

The following example defines a variable, basePath, to represent an application's current directory. It then passes it to the GetFullPath method to get a fully qualified path to the application's data directory.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = Environment.CurrentDirectory;
        string relativePath = "./data/output.xml";
 
        // Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/";
        
        string fullPath = Path.GetFullPath(relativePath, basePath);
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}");
        Console.WriteLine($"Fully qualified path:\n   {fullPath}");
    }
}
// The example displays the following output:
//   Current directory:
//      C:\Users\Public\Documents
//   Fully qualified path:
//      C:\Utilities\data\output.xml
Imports System.IO

Module Program
    Public Sub Main()
        Dim basePath As String = Environment.CurrentDirectory
        Dim relativePath As String = "./data/output.xml"
 
        ' Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/"
        
        Dim fullPath As String = Path.GetFullPath(relativePath, basePath)
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}")
        Console.WriteLine($"Fully qualified path:\n   {fullPath}")
    End Sub
End Module
' The example displays the following output:
'   Current directory:
'      C:\Users\Public\Documents
'   Fully qualified path:
'      C:\Utilities\data\output.xml

Remarks

If path is an empty path, the method returns basePath. If path is a fully qualified path, the method passes path to the GetFullPath(String) method and returns the result.

Use this method to return a deterministic path based on a specified volume and rooted directory when you're using relative paths. Using a predefined basePath rather than one based on the current drive directory guards against unwanted file paths caused by unexpected changes in the current drive and directory.

Applies to