In C#, you can convert an absolute path to a relative path using the System.IO.Path
class. The Path
class provides several methods for working with file and directory paths, including a method to convert absolute paths to relative paths.
Here's an example of how to convert an absolute path to a relative path in C#:
string absolutePath = "C:\\MyDirectory\\MySubDirectory\\MyFile.txt"; string baseDirectory = "C:\\MyDirectory"; string relativePath = Path.GetRelativePath(baseDirectory, absolutePath); Console.WriteLine(relativePath);
In this example, we have an absolute path to a file (C:\MyDirectory\MySubDirectory\MyFile.txt
) and a base directory (C:\MyDirectory
). We want to convert the absolute path to a relative path relative to the base directory.
We can use the Path.GetRelativePath()
method to convert the absolute path to a relative path. This method takes two parameters: the base directory and the absolute path. It returns a relative path that can be used to access the same file or directory relative to the base directory.
When we run this code, the relativePath
variable will contain the string "MySubDirectory\MyFile.txt"
, which is the relative path to the file from the base directory.
Note that the Path.GetRelativePath()
method was introduced in .NET Framework 4.6.1 and may not be available in earlier versions of .NET.
"C# convert absolute path to relative path"
Path.GetRelativePath
(C# 9.0 and later):using System.IO; string absolutePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(Directory.GetCurrentDirectory(), absolutePath);
Path.GetRelativePath
to convert an absolute path to a relative path in C#."C# get relative path from absolute path"
Uri
and Uri.MakeRelativeUri
:using System; string absolutePath = @"C:\Path\To\Your\File.txt"; Uri baseUri = new Uri(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar); Uri absoluteUri = new Uri(absolutePath); string relativePath = baseUri.MakeRelativeUri(absoluteUri).ToString();
Uri
and Uri.MakeRelativeUri
to obtain the relative path from an absolute path."Convert absolute file path to relative in C#"
Path.GetRelativePath
to convert an absolute file path to a relative path:using System.IO; string absoluteFilePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(Directory.GetCurrentDirectory(), absoluteFilePath);
Path.GetRelativePath
in C#."C# absolute to relative path conversion"
Path.GetFullPath
and Uri
:using System; using System.IO; string absolutePath = @"C:\Path\To\Your\File.txt"; string basePath = Directory.GetCurrentDirectory(); string relativePath = new Uri(basePath).MakeRelativeUri(new Uri(Path.GetFullPath(absolutePath))).ToString();
Path.GetFullPath
and Uri
in C#."C# make absolute path relative to base path"
Path.GetRelativePath
to make an absolute path relative to a base path:using System.IO; string basePath = @"C:\Path\To\Your"; string absolutePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(basePath, absolutePath);
Path.GetRelativePath
in C#."C# convert absolute directory path to relative"
Path.GetRelativePath
:using System.IO; string absoluteDirectoryPath = @"C:\Path\To\Your\Directory"; string relativePath = Path.GetRelativePath(Directory.GetCurrentDirectory(), absoluteDirectoryPath);
Path.GetRelativePath
in C#."Relative path from absolute path in C# with base directory"
Path.GetRelativePath
:using System.IO; string baseDirectory = @"C:\Path\To\Your"; string absolutePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(baseDirectory, absolutePath);
Path.GetRelativePath
in C#."C# get relative path from absolute path with Uri"
Uri
to get the relative path from an absolute path:using System; string absolutePath = @"C:\Path\To\Your\File.txt"; Uri baseUri = new Uri(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar); Uri absoluteUri = new Uri(absolutePath); string relativePath = baseUri.MakeRelativeUri(absoluteUri).ToString();
Uri
in C#."C# absolute to relative path conversion with base directory"
Path.GetRelativePath
:using System.IO; string baseDirectory = @"C:\Path\To\Your"; string absolutePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(baseDirectory, absolutePath);
Path.GetRelativePath
in C#."C# absolute to relative path without Uri"
Uri
:using System.IO; string basePath = @"C:\Path\To\Your"; string absolutePath = @"C:\Path\To\Your\File.txt"; string relativePath = Path.GetRelativePath(basePath, absolutePath);
Uri
for converting absolute to relative path in C#.chomp aio-write maven-ear-plugin angular-sanitizer thinktecture-ident-server asp.net-apicontroller rselenium spawn react-hot-loader spring-rabbit