Skip to content

Instantly share code, notes, and snippets.

@kuttsun
Last active April 27, 2017 13:41
Show Gist options
  • Save kuttsun/3596e9a2d93ebe80b51b3a734111a51b to your computer and use it in GitHub Desktop.
Save kuttsun/3596e9a2d93ebe80b51b3a734111a51b to your computer and use it in GitHub Desktop.
/// <summary>
/// 引数1のディレクトリから見た引数2のファイルへの相対パスを取得する
/// </summary>
/// <param name="uri1">基準となるディレクトリへの絶対パス(最後は\で終わっている必要あり)</param>
/// <param name="uri2">目的のファイルへの絶対パス</param>
/// <returns>引数1のディレクトリから見た引数2のファイルへの相対パス</returns>
/// <example>
/// GetRelativePath(@"C:\Windows\System\", @"C:\Windows\file.txt")
/// 出力:..\file.txt
/// </example>
string GetRelativePath(string uri1, string uri2)
{
Uri u1 = new Uri(uri1);
Uri u2 = new Uri(uri2);
Uri relativeUri = u1.MakeRelativeUri(u2);
string relativePath = relativeUri.ToString();
relativePath.Replace('/', '\\');
Console.WriteLine(relativePath);
return (relativePath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment