Ejemplo n.º 1
0
        internal static string WindowsDriveAdjustment(string path)
        {
            // three special cases to consider when a drive is specified
            if (path.Length < 2)
            {
                if (path.Length == 1 && (path[0] == '\\' || path[0] == '/'))
                {
                    return(Path.GetPathRoot(Directory.GetCurrentDirectory()));
                }
                return(path);
            }
            if ((path [1] != ':') || !Char.IsLetter(path [0]))
            {
                return(path);
            }

            string current = Directory.InsecureGetCurrentDirectory();

            // first, only the drive is specified
            if (path.Length == 2)
            {
                // then if the current directory is on the same drive
                if (current [0] == path [0])
                {
                    path = current;                     // we return it
                }
                else
                {
                    path = GetFullPathName(path);                     // we have to use the GetFullPathName Windows API
                }
            }
            else if ((path [2] != Path.DirectorySeparatorChar) && (path [2] != Path.AltDirectorySeparatorChar))
            {
                // second, the drive + a directory is specified *without* a separator between them (e.g. C:dir).
                // If the current directory is on the specified drive...
                if (current [0] == path [0])
                {
                    // then specified directory is appended to the current drive directory
                    path = Path.Combine(current, path.Substring(2, path.Length - 2));
                }
                else
                {
                    // we have to use the GetFullPathName Windows API
                    path = GetFullPathName(path);
                }
            }
            return(path);
        }
Ejemplo n.º 2
0
        internal static string WindowsDriveAdjustment(string path)
        {
            // two special cases to consider when a drive is specified
            if (path.Length < 2)
            {
                return(path);
            }
            if ((path [1] != ':') || !Char.IsLetter(path [0]))
            {
                return(path);
            }

            string current = Directory.InsecureGetCurrentDirectory();

            // first, only the drive is specified
            if (path.Length == 2)
            {
                // then if the current directory is on the same drive
                if (current [0] == path [0])
                {
                    path = current;                     // we return it
                }
                else
                {
                    path += '\\';
                }
            }
            else if ((path [2] != Path.DirectorySeparatorChar) && (path [2] != Path.AltDirectorySeparatorChar))
            {
                // second, the drive + a directory is specified *without* a separator between them (e.g. C:dir).
                // If the current directory is on the specified drive...
                if (current [0] == path [0])
                {
                    // then specified directory is appended to the current drive directory
                    path = Path.Combine(current, path.Substring(2, path.Length - 2));
                }
                else
                {
                    // if not, then just pretend there was a separator (Path.Combine won't work in this case)
                    path = String.Concat(path.Substring(0, 2), DirectorySeparatorStr, path.Substring(2, path.Length - 2));
                }
            }
            return(path);
        }
Ejemplo n.º 3
0
        // insecure - do not call directly
        internal static string InsecureGetFullPath(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Trim().Length == 0)
            {
                string msg = Locale.GetText("The specified path is not of a legal form (empty).");
                throw new ArgumentException(msg);
            }
#if !MOBILE
            // adjust for drives, i.e. a special case for windows
            if (Environment.IsRunningOnWindows)
            {
                path = WindowsDriveAdjustment(path);
            }
#endif
            // if the supplied path ends with a separator...
            char end = path [path.Length - 1];

            var canonicalize = true;
            if (path.Length >= 2 &&
                IsDirectorySeparator(path [0]) &&
                IsDirectorySeparator(path [1]))
            {
                if (path.Length == 2 || path.IndexOf(path [0], 2) < 0)
                {
                    throw new ArgumentException("UNC paths should be of the form \\\\server\\share.");
                }

                if (path [0] != DirectorySeparatorChar)
                {
                    path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar);
                }
            }
            else
            {
                if (!IsPathRooted(path))
                {
                    // avoid calling expensive CanonicalizePath when possible
                    if (!Environment.IsRunningOnWindows)
                    {
                        var start = 0;
                        while ((start = path.IndexOf('.', start)) != -1)
                        {
                            if (++start == path.Length || path [start] == DirectorySeparatorChar || path [start] == AltDirectorySeparatorChar)
                            {
                                break;
                            }
                        }
                        canonicalize = start > 0;
                    }

                    var cwd = Directory.InsecureGetCurrentDirectory();
                    if (cwd [cwd.Length - 1] == DirectorySeparatorChar)
                    {
                        path = cwd + path;
                    }
                    else
                    {
                        path = cwd + DirectorySeparatorChar + path;
                    }
                }
                else if (DirectorySeparatorChar == '\\' &&
                         path.Length >= 2 &&
                         IsDirectorySeparator(path [0]) &&
                         !IsDirectorySeparator(path [1]))                   // like `\abc\def'
                {
                    string current = Directory.InsecureGetCurrentDirectory();
                    if (current [1] == VolumeSeparatorChar)
                    {
                        path = current.Substring(0, 2) + path;
                    }
                    else
                    {
                        path = current.Substring(0, current.IndexOf('\\', current.IndexOfUnchecked("\\\\", 0, current.Length) + 1));
                    }
                }
            }

            if (canonicalize)
            {
                path = CanonicalizePath(path);
            }

            // if the original ended with a [Alt]DirectorySeparatorChar then ensure the full path also ends with one
            if (IsDirectorySeparator(end) && (path [path.Length - 1] != DirectorySeparatorChar))
            {
                path += DirectorySeparatorChar;
            }

            return(path);
        }