Windowsアプリケーションで相対パスを絶対パスに変換するにはどうすればよいですか?
ASP.NETでserver.MapPath()を使用できることはわかっています。しかし、Windowsアプリケーションで何ができるでしょうか。
つまり、それを処理できる.NET組み込み関数がある場合...
Windowsアプリケーションで相対パスを絶対パスに変換するにはどうすればよいですか?
ASP.NETでserver.MapPath()を使用できることはわかっています。しかし、Windowsアプリケーションで何ができるでしょうか。
つまり、それを処理できる.NET組み込み関数がある場合...
回答:
やってみました:
string absolute = Path.GetFullPath(relative);
?これは、実行可能ファイルを含むディレクトリではなく、プロセスの現在の作業ディレクトリを使用することに注意してください。それでも問題が解決しない場合は、質問を明確にしてください。
.exeからの相対パスを取得したい場合は、
string absolute = Path.Combine(Application.ExecutablePath, relative);
Path.Combineでもドライブ相対パスを処理することはできません。思われる初期パスを無視するだけです。私は自分の完全なソリューションを投稿しています。
..ゴミが発生します。
..それらに含まれるパス)は、.Netのすべてのファイル操作システムによって完全に受け入れられ、解決されます。気になる場合は、実行absolute = Path.GetFullPath(absolute)してください。
これは、異なるドライブのパス、ドライブ相対パス、および実際の相対パスで機能します。いや、basePath実際には絶対的ではない場合でも機能します。最終フォールバックとして常に現在の作業ディレクトリを使用します。
public static String GetAbsolutePath(String path)
{
return GetAbsolutePath(null, path);
}
public static String GetAbsolutePath(String basePath, String path)
{
if (path == null)
return null;
if (basePath == null)
basePath = Path.GetFullPath("."); // quick way of getting current working directory
else
basePath = GetAbsolutePath(null, basePath); // to be REALLY sure ;)
String finalPath;
// specific for windows paths starting on \ - they need the drive added to them.
// I constructed this piece like this for possible Mono support.
if (!Path.IsPathRooted(path) || "\\".Equals(Path.GetPathRoot(path)))
{
if (path.StartsWith(Path.DirectorySeparatorChar.ToString()))
finalPath = Path.Combine(Path.GetPathRoot(basePath), path.TrimStart(Path.DirectorySeparatorChar));
else
finalPath = Path.Combine(basePath, path);
}
else
finalPath = path;
// resolves any internal "..\" to get the true full path.
return Path.GetFullPath(finalPath);
}
Path.Combineです。これは簡単に修正できますが、作業ディレクトリの相対パスを解決するために頻繁に使用し、最初の引数としてnullを指定すると奇妙に見えるため、回避します。
少し古いトピックですが、誰かに役立つかもしれません。同様の問題を解決しましたが、私の場合、パスはテキストの最初ではありませんでした。
だからここに私の解決策があります:
public static class StringExtension
{
private const string parentSymbol = "..\\";
private const string absoluteSymbol = ".\\";
public static String AbsolutePath(this string relativePath)
{
string replacePath = AppDomain.CurrentDomain.BaseDirectory;
int parentStart = relativePath.IndexOf(parentSymbol);
int absoluteStart = relativePath.IndexOf(absoluteSymbol);
if (parentStart >= 0)
{
int parentLength = 0;
while (relativePath.Substring(parentStart + parentLength).Contains(parentSymbol))
{
replacePath = new DirectoryInfo(replacePath).Parent.FullName;
parentLength = parentLength + parentSymbol.Length;
};
relativePath = relativePath.Replace(relativePath.Substring(parentStart, parentLength), string.Format("{0}\\", replacePath));
}
else if (absoluteStart >= 0)
{
relativePath = relativePath.Replace(".\\", replacePath);
}
return relativePath;
}
}
例:
Data Source=.\Data\Data.sdf;Persist Security Info=False;
Data Source=..\..\bin\Debug\Data\Data.sdf;Persist Security Info=False;
Path.GetFullPath。\および.. \を自動的に解決します。また、AbsolutePath拡張関数を一般にStringクラスに追加しています...少しやり過ぎかもしれません。