How to convert a relative path to an absolute path in a Windows application?

后端 未结 4 1598
悲&欢浪女
悲&欢浪女 2020-12-02 17:41

How do I convert a relative path to an absolute path in a Windows application?

I know we can use server.MapPath() in ASP.NET. But what can we do in a Windows applica

相关标签:
4条回答
  • 2020-12-02 18:35

    It's a bit older topic, but it might be useful for someone. I have solved a similar problem, but in my case, the path was not at the beginning of the text.

    So here is my solution:

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

    Example:

    Data Source=.\Data\Data.sdf;Persist Security Info=False;
    Data Source=..\..\bin\Debug\Data\Data.sdf;Persist Security Info=False;
    
    0 讨论(0)
  • 2020-12-02 18:44

    This one works for paths on different drives, for drive-relative paths and for actual relative paths. Heck, it even works if the basePath isn't actually absolute; it always uses the current working directory as final fallback.

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 18:45

    Have you tried:

    string absolute = Path.GetFullPath(relative);
    

    ? Note that that will use the current working directory of the process, not the directory containing the executable. If that doesn't help, please clarify your question.

    0 讨论(0)
  • 2020-12-02 18:48

    If you want to get the path relative to your .exe then use

    string absolute = Path.Combine(Application.ExecutablePath, relative);
    
    0 讨论(0)
提交回复
热议问题