Ten działa na ścieżkach na różnych dyskach, na ścieżkach napędowych względne i dla rzeczywistych ścieżek względnych. Heck, działa nawet wtedy, gdy basePath
nie jest w rzeczywistości absolutny; zawsze używa bieżącego katalogu roboczego jako ostatecznego zastępczego.
public static String GetAbsolutePath(String relativePath, String basePath)
{
if (relativePath == null)
return null;
if (basePath == null)
basePath = Path.GetFullPath("."); // quick way of getting current working directory
else
basePath = GetAbsolutePath(basePath, null); // to be REALLY sure ;)
String path;
// 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(relativePath) || "\\".Equals(Path.GetPathRoot(relativePath)))
{
if (relativePath.StartsWith(Path.DirectorySeparatorChar.ToString()))
path = Path.Combine(Path.GetPathRoot(basePath), relativePath.TrimStart(Path.DirectorySeparatorChar));
else
path = Path.Combine(basePath, relativePath);
}
else
path = relativePath;
// resolves any internal "..\" to get the true full path.
return Path.GetFullPath(path);
}
Masz na myśli względną ścieżkę do bieżącego katalogu, innymi słowy katalog roboczy lub lokalizację pliku .exe? –
Istnieją dwa rodzaje względnych ścieżek. Jedna z postaci "A \ B \ C" i nie implikuje konkretnej bazy. Drugi ma postać ". \ A \ B" lub ".. \ A \ B"; są one związane z bieżącym katalogiem roboczym. – MSalters
@Amit Dhall: Przy okazji, dobrze jest zaakceptować i udzielić odpowiedzi, które ci pomogą. –