예제 #1
0
        public DiskSpaceInfo(string drivePath)
        {
            if (Utils.IsNullOrWhiteSpace(drivePath))
            {
                throw new ArgumentNullException("drivePath");
            }

            if (drivePath.Length == 1)
            {
                DriveName += Path.VolumeSeparatorChar;
            }
            else
            {
                DriveName = Path.GetPathRoot(drivePath, false);
            }

            if (Utils.IsNullOrWhiteSpace(DriveName))
            {
                throw new ArgumentException("Argument must be a drive letter (\"C\"), RootDir (\"C:\\\") or UNC path (\"\\\\server\\share\")");
            }

            // MSDN:
            // If this parameter is a UNC name, it must include a trailing backslash (for example, "\\MyServer\MyShare\").
            // Furthermore, a drive specification must have a trailing backslash (for example, "C:\").
            // The calling application must have FILE_LIST_DIRECTORY access rights for this directory.
            DriveName = Path.AddDirectorySeparator(DriveName, false);
        }
예제 #2
0
        public DriveInfo(string driveName)
        {
            if (Utils.IsNullOrWhiteSpace(driveName))
            {
                throw new ArgumentNullException("driveName");
            }

            if (driveName.Length == 1)
            {
                _name += Path.VolumeSeparatorChar;
            }
            else
            {
                _name = Path.GetPathRoot(driveName, false);
            }

            if (Utils.IsNullOrWhiteSpace(_name))
            {
                throw new ArgumentException("Argument must be a drive letter (\"C\"), RootDir (\"C:\\\") or UNC path (\"\\\\server\\share\")");
            }

            // If an exception is thrown, the original drivePath is used.
            _name = Path.AddDirectorySeparator(_name, false);


            // Initiate VolumeInfo() lazyload instance.
            _volumeInfo = new VolumeInfo(_name, false, true);

            // Initiate DiskSpaceInfo() lazyload instance.
            _dsi = new DiskSpaceInfo(_name, null, false, true);
        }
예제 #3
0
파일: VolumeInfo.cs 프로젝트: CDEApp/CDE
        public VolumeInfo(string volumeName)
        {
            if (Utils.IsNullOrWhiteSpace(volumeName))
            {
                throw new ArgumentNullException("volumeName");
            }

            if (volumeName.Length == 1)
            {
                Name += Path.VolumeSeparatorChar;
            }
            else
            {
                Name = Path.GetPathRoot(volumeName, false);
            }

            if (Utils.IsNullOrWhiteSpace(Name))
            {
                throw new ArgumentException("Argument must be a drive letter (\"C\"), RootDir (\"C:\\\") or UNC path (\"\\\\server\\share\")");
            }

            // If an exception is thrown, the original drivePath is used.
            Name = Path.AddDirectorySeparator(Name, false);

            _volumeHandle = null;
        }
예제 #4
0
        public IEnumerable <FileSystemEntryInfo> Enumerate()
        {
            // MSDN: Queue
            // Represents a first-in, first-out collection of objects.
            // The capacity of a Queue is the number of elements the Queue can hold.
            // As elements are added to a Queue, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize.
            // The growth factor is the number by which the current capacity is multiplied when a greater capacity is required. The growth factor is determined when the Queue is constructed.
            // The capacity of the Queue will always increase by a minimum value, regardless of the growth factor; a growth factor of 1.0 will not prevent the Queue from increasing in size.
            // If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Queue.
            // This constructor is an O(n) operation, where n is capacity.

            Queue <string> dirs = new Queue <string>(1000);

            // Removes the object at the beginning of your Queue.
            // The algorithmic complexity of this is O(1). It doesn't loop over elements.
            dirs.Enqueue(InputPath);

            // ChangeErrorMode is for the Win32 SetThreadErrorMode() method, used to suppress possible pop-ups.
            using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
                while (dirs.Count > 0)
                {
                    string path   = Path.AddDirectorySeparator(dirs.Dequeue(), false);
                    string pathLp = path + Path.WildcardStarMatchAll;
                    NativeMethods.Win32FindData win32FindData;

                    using (SafeFindFileHandle handle = FindFirstFile(pathLp, out win32FindData))
                    {
                        if (handle.IsInvalid && ContinueOnException)
                        {
                            continue;
                        }

                        do
                        {
                            string fileName = win32FindData.FileName;
                            switch (fileName)
                            {
                            // Skip entries.
                            case ".":
                            case "..":
                                break;

                            default:
                                string fullPathLp = path + fileName;
                                bool   fseiIsDir  = (win32FindData.FileAttributes & FileAttributes.Directory) == FileAttributes.Directory;

                                // Or not: Skip on reparse points here to cleanly separate regular directories from links.
                                if (SkipReparsePoints &&
                                    (win32FindData.FileAttributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                                {
                                    continue;
                                }

                                // If object is a directory, add it to the queue for later traversal.
                                if (fseiIsDir && _searchAllDirs)
                                {
                                    dirs.Enqueue(fullPathLp);
                                }


                                if (!(_nameFilter == null || (_nameFilter != null && _nameFilter.IsMatch(fileName))))
                                {
                                    break;
                                }

                                // Make sure the requested file system object type is returned.
                                // null = Return files and directories.
                                // true = Return only directories.
                                // false = Return only files.
                                switch (FileSystemObjectType)
                                {
                                case null:
                                    yield return(new FileSystemEntryInfo(win32FindData)
                                    {
                                        FullPath = fullPathLp
                                    });

                                    break;

                                case true:
                                    if (fseiIsDir)
                                    {
                                        yield return new FileSystemEntryInfo(win32FindData)
                                               {
                                                   FullPath = fullPathLp
                                               }
                                    }
                                    ;
                                    break;

                                case false:
                                    if (!fseiIsDir)
                                    {
                                        yield return new FileSystemEntryInfo(win32FindData)
                                               {
                                                   FullPath = fullPathLp
                                               }
                                    }
                                    ;
                                    break;
                                }
                                break;
                            }
                        } while (NativeMethods.FindNextFile(handle, out win32FindData));


                        uint lastError = (uint)Marshal.GetLastWin32Error();
                        switch (lastError)
                        {
                        case Win32Errors.ERROR_NO_MORE_FILES:
                            lastError = Win32Errors.NO_ERROR;
                            break;

                        case Win32Errors.ERROR_FILE_NOT_FOUND:
                        case Win32Errors.ERROR_PATH_NOT_FOUND:
                            if (lastError == Win32Errors.ERROR_FILE_NOT_FOUND && IsFolder)
                            {
                                lastError = Win32Errors.ERROR_PATH_NOT_FOUND;
                            }
                            break;
                        }

                        if (!ContinueOnException && lastError != Win32Errors.NO_ERROR)
                        {
                            NativeError.ThrowException(lastError, InputPath);
                        }
                    }
                }
        }