示例#1
0
        /// <summary>Lists the drives, directories and files of the file system path.</summary>
        /// <remarks>If an exception is thrown during the tries then the file system navigation is redirected to the parent directory.</remarks>
        private void ListFileSystemItems()
        {
            /*
             * Remove all columns.
             */

            GridView fileSystemGridView = (GridView)this.fileSystemListView.View;

            fileSystemGridView.Columns.Clear();

            fileSystemGridView.Columns.Add(this.nameGridViewColumn);

            /*
             * Remove all items.
             */

            this.fileSystemListViewItems.Clear();

            /*
             * Add relevant columns and items.
             */

            if (this.path.Length == 0)
            {
                if (this.browserSettings.HasVolumeLabel)
                {
                    fileSystemGridView.Columns.Add(this.volumeLabelGridViewColumn);
                }

                if (this.browserSettings.HasRootDirectory)
                {
                    fileSystemGridView.Columns.Add(this.rootDirectoryGridViewColumn);
                }

                if (this.browserSettings.HasDriveType)
                {
                    fileSystemGridView.Columns.Add(this.driveTypeGridViewColumn);
                }

                if (this.browserSettings.HasDriveFormat)
                {
                    fileSystemGridView.Columns.Add(this.driveFormatGridViewColumn);
                }

                if (this.browserSettings.HasAvailableFreeSpace)
                {
                    fileSystemGridView.Columns.Add(this.availableFreeSpaceGridViewColumn);
                }

                if (this.browserSettings.HasTotalFreeSpace)
                {
                    fileSystemGridView.Columns.Add(this.totalFreeSpaceGridViewColumn);
                }

                if (this.browserSettings.HasTotalSize)
                {
                    fileSystemGridView.Columns.Add(this.totalSizeGridViewColumn);
                }

                foreach (DriveInfo currentDriveInfo in DriveInfo.GetDrives())
                {
                    FileSystemItem currentFileSystemItem = new FileSystemItem()
                    {
                        ByteMultiple = this.browserSettings.ByteMultiple
                    };

                    currentFileSystemItem.Name = currentDriveInfo.Name;

                    if (this.browserSettings.HasVolumeLabel)
                    {
                        try
                        {
                            currentFileSystemItem.VolumeLabel = currentDriveInfo.VolumeLabel;
                        }
                        catch
                        {
                        }
                    }

                    if (this.browserSettings.HasRootDirectory)
                    {
                        DirectoryInfo currentDirectoryInfo = currentDriveInfo.RootDirectory;

                        currentFileSystemItem.RootDirectory = currentDirectoryInfo.Name;
                    }

                    if (this.browserSettings.HasDriveType)
                    {
                        switch (currentDriveInfo.DriveType)
                        {
                        case DriveType.CDRom:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.CDRom;

                            break;

                        case DriveType.Fixed:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.Fixed;

                            break;

                        case DriveType.Network:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.Network;

                            break;

                        case DriveType.NoRootDirectory:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.NoRootDirectory;

                            break;

                        case DriveType.Ram:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.Ram;

                            break;

                        case DriveType.Removable:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.Removable;

                            break;

                        case DriveType.Unknown:
                            currentFileSystemItem.DriveType = UserControls.Resources.DriveTypeEnumerations.Unknown;

                            break;

                        default:
                            currentFileSystemItem.DriveType = currentDriveInfo.DriveType.ToString();

                            break;
                        }
                    }

                    if (this.browserSettings.HasDriveFormat)
                    {
                        try
                        {
                            currentFileSystemItem.DriveFormat = currentDriveInfo.DriveFormat;
                        }
                        catch
                        {
                        }
                    }

                    if (this.browserSettings.HasAvailableFreeSpace)
                    {
                        try
                        {
                            currentFileSystemItem.AvailableFreeSpaceLong = currentDriveInfo.AvailableFreeSpace;
                        }
                        catch
                        {
                        }
                    }

                    if (this.browserSettings.HasTotalFreeSpace)
                    {
                        try
                        {
                            currentFileSystemItem.TotalFreeSpaceLong = currentDriveInfo.TotalFreeSpace;
                        }
                        catch
                        {
                        }
                    }

                    if (this.browserSettings.HasTotalSize)
                    {
                        try
                        {
                            currentFileSystemItem.TotalSizeLong = currentDriveInfo.TotalSize;
                        }
                        catch
                        {
                        }
                    }

                    this.fileSystemListViewItems.Add(currentFileSystemItem);
                }
            }
            else
            {
                fileSystemGridView.Columns.Add(this.sizeGridViewColumn);
                fileSystemGridView.Columns.Add(this.lastAccessTimeGridViewColumn);
                fileSystemGridView.Columns.Add(this.lastWriteTimeGridViewColumn);
                fileSystemGridView.Columns.Add(this.creationTimeGridViewColumn);

                DirectoryInfo listingDirectoryInfo = new DirectoryInfo(this.path);

                try
                {
                    foreach (DirectoryInfo currentDirectoryInfo in listingDirectoryInfo.GetDirectories())
                    {
                        this.fileSystemListViewItems.Add(new FileSystemItem()
                        {
                            ByteMultiple = this.browserSettings.ByteMultiple, FileSystemItemType = FileSystemItemType.Directory, Name = currentDirectoryInfo.Name, Accessed = currentDirectoryInfo.LastAccessTime, Written = currentDirectoryInfo.LastWriteTime, Created = currentDirectoryInfo.CreationTime
                        });
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    if (!this.isInitialNavigation)
                    {
                        this.isInitialNavigation = true;

                        this.NavigateFileSystem(string.Empty);

                        return;
                    }
                    else
                    {
                        ErrorWindow exceptionErrorWindow = new ErrorWindow(this, this.path, UserControls.Resources.Exceptions.DirectoryNotFoundMessage);

                        exceptionErrorWindow.ShowDialog();

                        this.NavigateFileSystem(PathManipulator.GetParentDirectory(this.path));
                    }

                    return;
                }
                catch (SecurityException)
                {
                    if (!this.isInitialNavigation)
                    {
                        this.isInitialNavigation = true;

                        this.NavigateFileSystem(string.Empty);

                        return;
                    }
                    else
                    {
                        ErrorWindow exceptionErrorWindow = new ErrorWindow(this, this.path, UserControls.Resources.Exceptions.SecurityMessage);

                        exceptionErrorWindow.ShowDialog();

                        this.NavigateFileSystem(PathManipulator.GetParentDirectory(this.path));
                    }

                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    if (!this.isInitialNavigation)
                    {
                        this.isInitialNavigation = true;

                        this.NavigateFileSystem(string.Empty);

                        return;
                    }
                    else
                    {
                        ErrorWindow exceptionErrorWindow = new ErrorWindow(this, this.path, UserControls.Resources.Exceptions.UnauthorizedAccessMessage);

                        exceptionErrorWindow.ShowDialog();

                        this.NavigateFileSystem(PathManipulator.GetParentDirectory(this.path));
                    }

                    return;
                }

                try
                {
                    foreach (FileInfo currentFileInfo in listingDirectoryInfo.GetFiles())
                    {
                        if (this.browserFilter.Process(currentFileInfo))
                        {
                            this.fileSystemListViewItems.Add(new FileSystemItem()
                            {
                                ByteMultiple = this.browserSettings.ByteMultiple, FileSystemItemType = FileSystemItemType.File, Name = currentFileInfo.Name, Length = currentFileInfo.Length, Accessed = currentFileInfo.LastAccessTime, Written = currentFileInfo.LastWriteTime, Created = currentFileInfo.CreationTime
                            });
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    if (!this.isInitialNavigation)
                    {
                        this.isInitialNavigation = true;

                        this.NavigateFileSystem(string.Empty);

                        return;
                    }
                    else
                    {
                        ErrorWindow exceptionErrorWindow = new ErrorWindow(this, this.path, UserControls.Resources.Exceptions.DirectoryNotFoundMessage);

                        exceptionErrorWindow.ShowDialog();

                        this.NavigateFileSystem(PathManipulator.GetParentDirectory(this.path));
                    }

                    return;
                }
            }

            /*
             * Resize the columns to fit the data.
             */

            foreach (GridViewColumn column in fileSystemGridView.Columns)
            {
                if (double.IsNaN(column.Width))
                {
                    column.Width = column.ActualWidth;
                }

                column.Width = double.NaN;
            }

            /*
             * Scroll to the top of the list view.
             */

            if (this.fileSystemListView.HasItems)
            {
                this.fileSystemListView.ScrollIntoView(this.fileSystemListView.Items[0]);
            }
        }