private const String NameField = "_name";  // For serialization

        public DriveInfo(String driveName)
        {
            if (driveName == null)
            {
                throw new ArgumentNullException("driveName");
            }
            if (driveName.Length == 1)
            {
                _name = driveName + ":\\";
            }
            else
            {
                // GetPathRoot does not check all invalid characters
                Path.CheckInvalidPathChars(driveName);
                _name = Path.GetPathRoot(driveName);
                // Disallow null or empty drive letters and UNC paths
                if (_name == null || _name.Length == 0 || _name.StartsWith("\\\\", StringComparison.Ordinal))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDriveLetterOrRootDir"));
                }
            }
            if (_name.Length == 2 && _name[1] == ':')
            {
                _name = _name + "\\";
            }

            // Now verify that the drive letter could be a real drive name.
            // On Windows this means it's between A and Z, ignoring case.
            // On a Unix platform, perhaps this should be a device name with
            // a partition like /dev/hdc0, or possibly a mount point.
            char letter = driveName[0];

            if (!((letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z')))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDriveLetterOrRootDir"));
            }

            // Now do a security check.
            String demandPath = _name + '.';

            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPath).Demand();
        }
Esempio n. 2
0
 public static bool HasExtension(string path)
 {
     if (path != null)
     {
         Path.CheckInvalidPathChars(path, false);
         int length = path.Length;
         while (--length >= 0)
         {
             char ch = path[length];
             if ((int)ch == 46)
             {
                 return(length != path.Length - 1);
             }
             if ((int)ch == (int)Path.DirectorySeparatorChar || (int)ch == (int)Path.AltDirectorySeparatorChar || (int)ch == (int)Path.VolumeSeparatorChar)
             {
                 break;
             }
         }
     }
     return(false);
 }
Esempio n. 3
0
 public static string GetDirectoryName(string path)
 {
     if (path != null)
     {
         Path.CheckInvalidPathChars(path);
         path = Path.NormalizePath(path, false);
         int rootLength = Path.GetRootLength(path);
         if (path.Length > rootLength)
         {
             int length = path.Length;
             if (length == rootLength)
             {
                 return((string)null);
             }
             do
             {
                 ;
             }while (length > rootLength && (int)path[--length] != (int)Path.DirectorySeparatorChar && (int)path[length] != (int)Path.AltDirectorySeparatorChar);
             return(path.Substring(0, length));
         }
     }
     return((string)null);
 }
Esempio n. 4
0
        internal static unsafe string NormalizePath(string path, bool fullCheck, int maxPathLength, bool expandShortPaths)
        {
            if (fullCheck)
            {
                path = path.TrimEnd(Path.TrimEndChars);
                Path.CheckInvalidPathChars(path, false);
            }
            int        index1 = 0;
            PathHelper pathHelper;

            if (path.Length + 1 <= Path.MaxPath)
            {
                char *charArrayPtr = stackalloc char[Path.MaxPath];
                pathHelper = new PathHelper(charArrayPtr, Path.MaxPath);
            }
            else
            {
                pathHelper = new PathHelper(path.Length + Path.MaxPath, maxPathLength);
            }
            uint num1  = 0;
            uint num2  = 0;
            bool flag1 = false;
            uint num3  = 0;
            int  num4  = -1;
            bool flag2 = false;
            bool flag3 = true;
            int  num5  = 0;
            bool flag4 = false;

            if (path.Length > 0 && ((int)path[0] == (int)Path.DirectorySeparatorChar || (int)path[0] == (int)Path.AltDirectorySeparatorChar))
            {
                pathHelper.Append('\\');
                ++index1;
                num4 = 0;
            }
            for (; index1 < path.Length; ++index1)
            {
                char ch1 = path[index1];
                if ((int)ch1 == (int)Path.DirectorySeparatorChar || (int)ch1 == (int)Path.AltDirectorySeparatorChar)
                {
                    if ((int)num3 == 0)
                    {
                        if (num2 > 0U)
                        {
                            int index2 = num4 + 1;
                            if ((int)path[index2] != 46)
                            {
                                throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                            }
                            if (num2 >= 2U)
                            {
                                if (flag2 && num2 > 2U)
                                {
                                    throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                                }
                                if ((int)path[index2 + 1] == 46)
                                {
                                    for (int index3 = index2 + 2; (long)index3 < (long)index2 + (long)num2; ++index3)
                                    {
                                        if ((int)path[index3] != 46)
                                        {
                                            throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                                        }
                                    }
                                    num2 = 2U;
                                }
                                else
                                {
                                    if (num2 > 1U)
                                    {
                                        throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                                    }
                                    num2 = 1U;
                                }
                            }
                            if ((int)num2 == 2)
                            {
                                pathHelper.Append('.');
                            }
                            pathHelper.Append('.');
                            flag1 = false;
                        }
                        if (num1 > 0U & flag3 && index1 + 1 < path.Length && ((int)path[index1 + 1] == (int)Path.DirectorySeparatorChar || (int)path[index1 + 1] == (int)Path.AltDirectorySeparatorChar))
                        {
                            pathHelper.Append(Path.DirectorySeparatorChar);
                        }
                    }
                    num2 = 0U;
                    num1 = 0U;
                    if (!flag1)
                    {
                        flag1 = true;
                        pathHelper.Append(Path.DirectorySeparatorChar);
                    }
                    num3  = 0U;
                    num4  = index1;
                    flag2 = false;
                    flag3 = false;
                    if (flag4)
                    {
                        pathHelper.TryExpandShortFileName();
                        flag4 = false;
                    }
                    int num6 = pathHelper.Length - 1;
                    int num7 = num5;
                    if (num6 - num7 > Path.MaxDirectoryLength)
                    {
                        throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                    }
                    num5 = num6;
                }
                else if ((int)ch1 == 46)
                {
                    ++num2;
                }
                else if ((int)ch1 == 32)
                {
                    ++num1;
                }
                else
                {
                    if ((int)ch1 == 126 & expandShortPaths)
                    {
                        flag4 = true;
                    }
                    flag1 = false;
                    if (flag3 && (int)ch1 == (int)Path.VolumeSeparatorChar)
                    {
                        char ch2 = index1 > 0 ? path[index1 - 1] : ' ';
                        if (((int)num2 != 0 || num3 < 1U ? 0 : ((int)ch2 != 32 ? 1 : 0)) == 0)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                        }
                        flag2 = true;
                        if (num3 > 1U)
                        {
                            int index2 = 0;
                            while (index2 < pathHelper.Length && (int)pathHelper[index2] == 32)
                            {
                                ++index2;
                            }
                            if ((long)num3 - (long)index2 == 1L)
                            {
                                pathHelper.Length = 0;
                                pathHelper.Append(ch2);
                            }
                        }
                        num3 = 0U;
                    }
                    else
                    {
                        num3 += 1U + num2 + num1;
                    }
                    if (num2 > 0U || num1 > 0U)
                    {
                        int num6 = num4 >= 0 ? index1 - num4 - 1 : index1;
                        if (num6 > 0)
                        {
                            for (int index2 = 0; index2 < num6; ++index2)
                            {
                                pathHelper.Append(path[num4 + 1 + index2]);
                            }
                        }
                        num2 = 0U;
                        num1 = 0U;
                    }
                    pathHelper.Append(ch1);
                    num4 = index1;
                }
            }
            if (pathHelper.Length - 1 - num5 > Path.MaxDirectoryLength)
            {
                throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
            }
            if ((int)num3 == 0 && num2 > 0U)
            {
                int index2 = num4 + 1;
                if ((int)path[index2] != 46)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                }
                if (num2 >= 2U)
                {
                    if (flag2 && num2 > 2U)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                    }
                    if ((int)path[index2 + 1] == 46)
                    {
                        for (int index3 = index2 + 2; (long)index3 < (long)index2 + (long)num2; ++index3)
                        {
                            if ((int)path[index3] != 46)
                            {
                                throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                            }
                        }
                        num2 = 2U;
                    }
                    else
                    {
                        if (num2 > 1U)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
                        }
                        num2 = 1U;
                    }
                }
                if ((int)num2 == 2)
                {
                    pathHelper.Append('.');
                }
                pathHelper.Append('.');
            }
            if (pathHelper.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegal"));
            }
            if (fullCheck && (pathHelper.OrdinalStartsWith("http:", false) || pathHelper.OrdinalStartsWith("file:", false)))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_PathUriFormatNotSupported"));
            }
            if (flag4)
            {
                pathHelper.TryExpandShortFileName();
            }
            int num8 = 1;

            if (fullCheck)
            {
                num8 = pathHelper.GetFullPathName();
                bool flag5 = false;
                for (int index2 = 0; index2 < pathHelper.Length && !flag5; ++index2)
                {
                    if ((int)pathHelper[index2] == 126 & expandShortPaths)
                    {
                        flag5 = true;
                    }
                }
                if (flag5 && !pathHelper.TryExpandShortFileName())
                {
                    int lastSlash = -1;
                    for (int index2 = pathHelper.Length - 1; index2 >= 0; --index2)
                    {
                        if ((int)pathHelper[index2] == (int)Path.DirectorySeparatorChar)
                        {
                            lastSlash = index2;
                            break;
                        }
                    }
                    if (lastSlash >= 0)
                    {
                        if (pathHelper.Length >= maxPathLength)
                        {
                            throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                        }
                        int lenSavedName = pathHelper.Length - lastSlash - 1;
                        pathHelper.Fixup(lenSavedName, lastSlash);
                    }
                }
            }
            if (num8 != 0 && pathHelper.Length > 1 && ((int)pathHelper[0] == 92 && (int)pathHelper[1] == 92))
            {
                int index2;
                for (index2 = 2; index2 < num8; ++index2)
                {
                    if ((int)pathHelper[index2] == 92)
                    {
                        ++index2;
                        break;
                    }
                }
                if (index2 == num8)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_PathIllegalUNC"));
                }
                if (pathHelper.OrdinalStartsWith("\\\\?\\globalroot", true))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_PathGlobalRoot"));
                }
            }
            if (pathHelper.Length >= maxPathLength)
            {
                throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
            }
            if (num8 == 0)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode == 0)
                {
                    errorCode = 161;
                }
                __Error.WinIOError(errorCode, path);
                return((string)null);
            }
            string a = pathHelper.ToString();

            if (string.Equals(a, path, StringComparison.Ordinal))
            {
                a = path;
            }
            return(a);
        }