Beispiel #1
0
        private static string CanonicalizePath(string path)
        {
            if (path == null)
            {
                return(path);
            }
            if (Environment.IsRunningOnWindows)
            {
                path = path.Trim();
            }
            if (path.Length == 0)
            {
                return(path);
            }
            string pathRoot = Path.GetPathRoot(path);

            string[] array = path.Split(new char[]
            {
                Path.DirectorySeparatorChar,
                Path.AltDirectorySeparatorChar
            });
            int  num  = 0;
            bool flag = Environment.IsRunningOnWindows && pathRoot.Length > 2 && Path.IsDsc(pathRoot[0]) && Path.IsDsc(pathRoot[1]);
            int  num2 = (!flag) ? 0 : 3;

            for (int i = 0; i < array.Length; i++)
            {
                if (Environment.IsRunningOnWindows)
                {
                    array[i] = array[i].TrimEnd(new char[0]);
                }
                if (!(array[i] == ".") && (i == 0 || array[i].Length != 0))
                {
                    if (array[i] == "..")
                    {
                        if (num > num2)
                        {
                            num--;
                        }
                    }
                    else
                    {
                        array[num++] = array[i];
                    }
                }
            }
            if (num == 0 || (num == 1 && array[0] == string.Empty))
            {
                return(pathRoot);
            }
            string text = string.Join(Path.DirectorySeparatorStr, array, 0, num);

            if (!Environment.IsRunningOnWindows)
            {
                return(text);
            }
            if (flag)
            {
                text = Path.DirectorySeparatorStr + text;
            }
            if (!Path.SameRoot(pathRoot, text))
            {
                text = pathRoot + text;
            }
            if (flag)
            {
                return(text);
            }
            if (!Path.IsDsc(path[0]) && Path.SameRoot(pathRoot, path))
            {
                if (text.Length <= 2 && !text.EndsWith(Path.DirectorySeparatorStr))
                {
                    text += Path.DirectorySeparatorChar;
                }
                return(text);
            }
            string currentDirectory = Directory.GetCurrentDirectory();

            if (currentDirectory.Length > 1 && currentDirectory[1] == Path.VolumeSeparatorChar)
            {
                if (text.Length == 0 || Path.IsDsc(text[0]))
                {
                    text += '\\';
                }
                return(currentDirectory.Substring(0, 2) + text);
            }
            if (Path.IsDsc(currentDirectory[currentDirectory.Length - 1]) && Path.IsDsc(text[0]))
            {
                return(currentDirectory + text.Substring(1));
            }
            return(currentDirectory + text);
        }