rutas ruta relativo relativa informatica entre diferencia absoluto absoluta c#

c# - relativo - ruta relativa y absoluta java



¿Cómo convertir una ruta relativa a una ruta absoluta en una aplicación de Windows? (4)

Éste funciona para rutas en diferentes unidades, para rutas relativas a unidades y para rutas relativas reales. Diablos, incluso funciona si el basePath no es realmente absoluto; siempre usa el directorio de trabajo actual como respaldo final.

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); }

¿Cómo convierto una ruta relativa a una ruta absoluta en una aplicación de Windows?

Sé que podemos usar server.MapPath () en ASP.NET. Pero, ¿qué podemos hacer en una aplicación de Windows?

Quiero decir, si hay una función incorporada de .NET que pueda manejar eso ...


Es un tema un poco viejo, pero podría ser útil para alguien. He resuelto un problema similar, pero en mi caso, el camino no estaba al principio del texto.

Así que aquí está mi solución:

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; } }

Ejemplo:

Data Source=./Data/Data.sdf;Persist Security Info=False; Data Source=../../bin/Debug/Data/Data.sdf;Persist Security Info=False;


Has probado:

string absolute = Path.GetFullPath(relative);

? Tenga en cuenta que usará el directorio de trabajo actual del proceso, no el directorio que contiene el ejecutable. Si eso no ayuda, aclare su pregunta.


Si desea obtener la ruta relativa a su .exe, utilice

string absolute = Path.Combine(Application.ExecutablePath, relative);