C# Tips


相対パスを取得するには


相対パスを取得するにはUriクラスを使用します。

.NET Framework 2.0からURIクラスの使用方法が変わりました。
//.NET Framework 2.0

Uri startupPath, targetPath;

startupPath = new Uri(Application.StartupPath + "\\");
targetPath = new Uri(value);

//startupPathから見た、targetPathを相対パスで取得する
Console.WriteLine(startupPath.MakeRelativeUri(targetPath).ToString());


//---------------------------------------------------------------

//.NET Framework 1.1

Uri startupPath, targetPath;

startupPath = new Uri(Application.StartupPath + "\\", true);
targetPath = new Uri(value, true);

//startupPathから見た、targetPathを相対パスで取得する
Console.WriteLine(startupPath.MakeRelative(targetPath));


目次に戻る
Copyright(c) 2008 WoodenSoldier Software