In C#, Path.GetFullPath() turns a path into a full path. A relative path such as "notes.txt" gets the working directory added in front. Dot segments are cleaned up: "a/../b" becomes .../b, and "./x" becomes .../x.
A second form, GetFullPath(path, basePath), uses a base folder that you choose instead of the working directory. It is available in .NET Core 2.1 and newer. In both forms the method works on text: the file or folder does not have to exist.
This is a static method of System.IO.Path. Call it as Path.GetFullPath(...); there is no new Path(). It does not open or check anything on disk, but for a relative path it reads the current working directory (and on Windows, the current drive). So the same input can give different answers in different folders.
Syntax
Pass a path, and optionally a fully qualified base path.
C#
string full = Path.GetFullPath("a/../b"); // working directory + /b
Console.WriteLine(Path.GetFileName(full)); // b
Console.WriteLine(Path.IsPathRooted(full)); // True
Call
What it does
GetFullPath("notes.txt")
Working directory plus notes.txt.
GetFullPath("a/../b")
The a/.. pair cancels out: working directory plus b.
GetFullPath("x.txt", basePath)
basePath plus x.txt. The base must be fully qualified.
GetFullPath("/x/../y")
Linux: /y. Windows: the current drive plus \y.
Symbolic links are not followed, and letter case is kept. The result uses the separator of the current system, so print it through Replace('\\', '/') for identical text everywhere.
Library declarations
C#
public static string GetFullPath(string path);
public static string GetFullPath(string path, string basePath);
Parameters
Parameter
Description
path
The path to complete. It can be relative, rooted, or already full. It cannot be null or empty.
basePath
Optional. A fully qualified base folder to start from. A relative base throws ArgumentException.
Return Value
Return Value
Description
string
The full path, with . and .. segments resolved and the separators of the current system.
C# Examples
Example 1 — Turn a Relative Path Into a Full One
Clean up a/../b, then show that the result is the working directory plus b. No full path is printed, because it would differ on every machine.
C#
using System;
using System.IO;
class Program {
static void Main() {
string full = Path.GetFullPath("a/../b");
Console.WriteLine(Path.GetFileName(full));
Console.WriteLine(Path.IsPathRooted(full));
Console.WriteLine(full.Contains(".."));
string byHand = Path.Combine(Directory.GetCurrentDirectory(), "b");
Console.WriteLine(full == byHand);
}
}
Output
b
True
False
True
How It Works
The dots cancel.a/.. goes down into a and straight back up, so only b is left. GetFileName shows that the last part is b.
Now it is rooted. The working directory was added in front, so the result starts at a root and IsPathRooted is True.
No dot segments remain. The text does not contain .. any more, so Contains("..") is False.
Same as doing it by hand.Combine(Directory.GetCurrentDirectory(), "b") builds the very same text, which proves what GetFullPath did. The printed values are facts, not machine-specific paths.
Example 2 — Resolve a Path Against Your Own Base Folder
Use the two-argument form with a base in the temp folder, then see what happens when the base is not fully qualified.
C#
using System;
using System.IO;
class Program {
static void Main() {
string basePath = Path.Combine(Path.GetTempPath(), "codetofun-fullpath-base");
string full = Path.GetFullPath("../shared/x.txt", basePath);
Console.WriteLine(Path.GetFileName(full));
Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(full)));
Console.WriteLine(Path.GetRelativePath(basePath, full).Replace('\\', '/'));
try {
Path.GetFullPath("x.txt", "relative/base");
} catch (ArgumentException ex) {
Console.WriteLine("Caught: " + ex.GetType().Name);
}
}
}
A base you control.basePath is a folder inside the temp folder. The relative path ../shared/x.txt is resolved from there and not from the working directory.
Read the pieces. The file name is x.txt, and its folder is named shared. Both are printed through GetFileName, so no machine-specific path appears.
The way back.GetRelativePath turns the full path into ../shared/x.txt again. The .. moved one level up from the base. Replace keeps the separator the same everywhere.
The base must be complete."relative/base" is not fully qualified, so the method throws ArgumentException.
Example 3 — Dot Segments Disappear, and Nothing Is Created
Resolve a messy path with . and .. segments, then confirm that no file or folder appeared.
C#
using System;
using System.IO;
class Program {
static void Main() {
string baseDir = Path.Combine(Path.GetTempPath(), "codetofun-fullpath-dots");
string full = Path.GetFullPath("./docs/../c/./file.txt", baseDir);
Console.WriteLine(Path.GetRelativePath(baseDir, full).Replace('\\', '/'));
Console.WriteLine("Has .. left: " + full.Contains(".."));
Console.WriteLine("File exists: " + File.Exists(full));
Console.WriteLine("Base folder exists: " + Directory.Exists(baseDir));
}
}
Output
c/file.txt
Has .. left: False
File exists: False
Base folder exists: False
How It Works
A messy input../docs/../c/./file.txt enters docs, leaves it again, then goes into c. The single dots mean "this folder" and do nothing.
A clean result. Measured from the base, the cleaned path is c/file.txt. GetRelativePath removes the long temp folder, so the line is the same on every machine.
No dots remain.Contains("..") is False, because the method resolved every segment.
Only text, no disk. The file and the base folder were never created, so both existence checks print False. A full path can point to something that is not there.
Frequently Asked Questions
It converts a path into a full path by adding the working directory in front of a relative path and resolving "." and ".." segments. It does not check that the path exists.
No. It only does text work, so a path to a missing file still gets a full path. Use Path.Exists, File.Exists, or Directory.Exists to check the disk.
For a relative path it takes Directory.GetCurrentDirectory() and joins the path to it. The same relative path gives a different result if the working directory changes.
GetFullPath(path, basePath) resolves a relative path against a base you choose, not the working directory. The base must be fully qualified, or ArgumentException is thrown. It is in .NET Core 2.1 and newer.
No. GetFullPath("../x", basePath) simply goes one level above the base. To check containment, compare the result with your base folder using StartsWith, after adding a trailing separator to the base.