Path.GetRelativePathメソッドを使用することで、目的のパスと基準(あるいは基点、base)となるパスの2つの絶対パスから相対パスを取得することができます。
基準となるパスを表す引数relativeToは、常にディレクトリを表すものとして扱われるため、relativeToがディレクトリ区切り文字で終端されているかどうかによって結果が変わることはありません。 異なるドライブ間のパス同士の場合は、絶対パスとして結果が返されます。 (F:\from
→T:\to
の場合はT:\to
) 同一ドライブ内のパス同士の場合でも、カレントドライブからの相対パス形式とはならず、ディレクトリ間の相対パスとして結果が返されます。 (E:\from
→E:\to
の場合は\to
ではなく..\to
)
なお、GetRelativePathメソッドは.NET Standard 2.1/.NET Core 2.0以降で使用できます。 GetRelativePathメソッドが使用できない場合は、Uriクラスを使ってURIとして処理することにより相対パスを得られます。
using System;
using System.IO;
class Sample {
static void Main()
{
foreach (var (pathFrom, pathTo) in new[] {
// Windows形式のパス
(@"E:\", @"E:\dir"),
(@"E:\", @"E:\dir\"),
(@"E:\", @"E:\dir\file.txt"),
(@"E:\dir", @"E:\"),
(@"E:\dir\", @"E:\"),
(@"E:\dir\file.txt", @"E:\"),
(@"E:\dir-from", @"E:\dir-to"),
(@"E:\dir-from", @"E:\dir-to\file-to.txt"),
(@"E:\dir-from\file-from.txt", @"E:\dir-to\file-to.txt"),
(@"E:\", @"C:\"),
(@"E:\dir-from\file-from.txt", @"C:\dir-to\file-to.txt"),
// UNIX形式のパス
("/", "/dir"),
("/", "/dir/"),
("/", "/dir/file.txt"),
("/dir", "/"),
("/dir/", "/"),
("/dir/file.txt", "/"),
("/dir-from", "/dir-to"),
("/dir-from", "/dir-to/file-to.txt"),
("/dir-from/file-from.txt", "/dir-to/file-to.txt"),
}) {
Console.WriteLine($"path from = {pathFrom}");
Console.WriteLine($"path to = {pathTo}");
Console.WriteLine($"relative = {Path.GetRelativePath(pathFrom, pathTo)}");
Console.WriteLine();
}
}
}
path from = E:\ path to = E:\dir relative = dir path from = E:\ path to = E:\dir\ relative = dir\ path from = E:\ path to = E:\dir\file.txt relative = dir\file.txt path from = E:\dir path to = E:\ relative = .. path from = E:\dir\ path to = E:\ relative = .. path from = E:\dir\file.txt path to = E:\ relative = ..\.. path from = E:\dir-from path to = E:\dir-to relative = ..\dir-to path from = E:\dir-from path to = E:\dir-to\file-to.txt relative = ..\dir-to\file-to.txt path from = E:\dir-from\file-from.txt path to = E:\dir-to\file-to.txt relative = ..\..\dir-to\file-to.txt path from = E:\ path to = C:\ relative = C:\ path from = E:\dir-from\file-from.txt path to = C:\dir-to\file-to.txt relative = C:\dir-to\file-to.txt path from = / path to = /dir relative = dir path from = / path to = /dir/ relative = dir\ path from = / path to = /dir/file.txt relative = dir\file.txt path from = /dir path to = / relative = .. path from = /dir/ path to = / relative = .. path from = /dir/file.txt path to = / relative = ..\.. path from = /dir-from path to = /dir-to relative = ..\dir-to path from = /dir-from path to = /dir-to/file-to.txt relative = ..\dir-to\file-to.txt path from = /dir-from/file-from.txt path to = /dir-to/file-to.txt relative = ..\..\dir-to\file-to.txt
path from = E:\ path to = E:\dir relative = ../E:\dir path from = E:\ path to = E:\dir\ relative = ../E:\dir\ path from = E:\ path to = E:\dir\file.txt relative = ../E:\dir\file.txt path from = E:\dir path to = E:\ relative = ../E:\ path from = E:\dir\ path to = E:\ relative = ../E:\ path from = E:\dir\file.txt path to = E:\ relative = ../E:\ path from = E:\dir-from path to = E:\dir-to relative = ../E:\dir-to path from = E:\dir-from path to = E:\dir-to\file-to.txt relative = ../E:\dir-to\file-to.txt path from = E:\dir-from\file-from.txt path to = E:\dir-to\file-to.txt relative = ../E:\dir-to\file-to.txt path from = E:\ path to = C:\ relative = ../C:\ path from = E:\dir-from\file-from.txt path to = C:\dir-to\file-to.txt relative = ../C:\dir-to\file-to.txt path from = / path to = /dir relative = dir path from = / path to = /dir/ relative = dir/ path from = / path to = /dir/file.txt relative = dir/file.txt path from = /dir path to = / relative = .. path from = /dir/ path to = / relative = .. path from = /dir/file.txt path to = / relative = ../.. path from = /dir-from path to = /dir-to relative = ../dir-to path from = /dir-from path to = /dir-to/file-to.txt relative = ../dir-to/file-to.txt path from = /dir-from/file-from.txt path to = /dir-to/file-to.txt relative = ../../dir-to/file-to.txt
絶対パスを取得するにはGetFullPathメソッドを使用します。
実行環境によってパスにおける大文字小文字の違いを意識/無視するかは異なり、例えばWindowsではdir
とDIR
は同じパスを表すと判断される一方、それ以外では異なるパスと判断される場合があります。
GetRelativePathメソッドは実行環境での大文字小文字の扱い方に応じて相対パスを作成します。 GetRelativePathメソッドでは大文字小文字の違いを意識/無視するかどうか、あるいはパスの比較に用いるIEqualityComparer<String>等を明示的に指定することはできません。
なお、GetRelativePathメソッドでのパス間の比較は、StringComparison.Ordinal/OrdinalIgnoreCaseでの比較が行われます。
厳密には、大文字小文字の扱われ方はファイルシステムごとに異なる一方、GetRelativePathメソッドはパスがどのファイルシステム上のエントリを表すものか、またそのファイルシステムでの扱われ方については関知しません。