コード例 #1
0
        public override void Scan()
        {
            BlockPerfStats stats = new BlockPerfStats();

            stats.Begin();

            Results = new PathScanResults <string>();

            //
            // Add sources.
            //
            foreach (var entry in Plan.SelectedSources)
            {
                try
                {
                    switch (entry.Type)
                    {
                    default:
                        throw new InvalidOperationException("Unhandled EntryType");

                    case EntryType.DRIVE:
                    {
                        var dir = new ZetaLongPaths.ZlpDirectoryInfo(new DriveInfo(entry.Path).RootDirectory.FullName);
                        AddDirectory(dir);
                        break;
                    }

                    case EntryType.FOLDER:
                    {
                        var dir = new ZetaLongPaths.ZlpDirectoryInfo(entry.Path);
                        AddDirectory(dir);
                        break;
                    }

                    case EntryType.FILE:
                    {
                        var file = new ZetaLongPaths.ZlpFileInfo(entry.Path);
                        AddFile(file);
                        break;
                    }
                    }
                }
                catch (OperationCanceledException)
                {
                    throw;                     // Rethrow!
                }
                catch (Exception ex)
                {
                    HandleException(entry.Type, entry.Path, ex);
                }
            }

            stats.End();
        }
コード例 #2
0
        private void AddDirectory(ZetaLongPaths.ZlpDirectoryInfo directory)
        {
            CancellationToken.ThrowIfCancellationRequested();

            try
            {
                // IMPORTANT: The condition below, `if (!directory.Exists)`, doesn't guarantee the
                // directory will exist after this statement! We probably shouldn't try to detect this but
                // handle any exceptions that may be raised.
                if (!directory.Exists)
                {
                    // DirectoryInfo.FullName may throw:
                    //    System.IO.PathTooLongException
                    //    System.Security.SecurityException
                    logger.Warn("Directory {0} does not exist", directory.FullName);
                    return;
                }

                ZetaLongPaths.ZlpFileInfo[] files = directory.GetFiles();                 // System.IO.DirectoryNotFoundException
                // Add all files from this directory.
                foreach (var file in files)
                {
                    AddFile(file);
                }

                ZetaLongPaths.ZlpDirectoryInfo[] directories = directory.GetDirectories();
                // Add all sub-directories recursively.
                foreach (var subdir in directories)
                {
                    AddDirectory(subdir);
                }
            }
            catch (OperationCanceledException)
            {
                throw;                 // Rethrow!
            }
            catch (Exception ex)
            {
                HandleException(EntryType.FOLDER, directory.FullName, ex);
            }
        }
コード例 #3
0
        // May throw System.SystemException
        private void PopuplateDirectory(EntryInfo info)
        {
            if (info.Type != TypeEnum.DRIVE && info.Type != TypeEnum.FOLDER)
            {
                throw new ArgumentException("Unexpected TypeEnum", "info.Type");
            }

            ZetaLongPaths.ZlpDirectoryInfo dir = info.Type == TypeEnum.DRIVE
                                ? new ZetaLongPaths.ZlpDirectoryInfo(new DriveInfo(info.Path).RootDirectory.FullName)
                                : new ZetaLongPaths.ZlpDirectoryInfo(info.Path);

            ZetaLongPaths.ZlpDirectoryInfo[] subDirs  = dir.GetDirectories();
            ZetaLongPaths.ZlpFileInfo[]      subFiles = dir.GetFiles();
            foreach (ZetaLongPaths.ZlpDirectoryInfo subDir in subDirs)
            {
                EntryInfo          subInfo       = new EntryInfo(TypeEnum.FOLDER, subDir.Name, subDir.FullName + System.IO.Path.DirectorySeparatorChar);
                FileSystemTreeNode subFolderNode = AddFolderNode(subInfo);
            }
            foreach (var file in subFiles)
            {
                EntryInfo          subInfo     = new EntryInfo(TypeEnum.FILE, file.Name, file.FullName);
                FileSystemTreeNode subFileNode = AddFileNode(subInfo);
            }
        }
コード例 #4
0
        protected bool ProcessFolder(string activeFolder, DeleteFilesCommandLineParser parser)
        {
            ZetaLongPaths.ZlpFileInfo[] files;

            try
            {
                var folder = new ZetaLongPaths.ZlpDirectoryInfo(activeFolder);
                files = folder.GetFiles(parser.FileSpec);
            }
            catch (Exception e)
            {
                OnShowMessage(Resources.ErrorOpening + activeFolder + ". " + e.GetBaseException().Message);
                return(false);
            }
            bool success = true;

            foreach (var fi in files)
            {
                //var fi = new FileInfo(file);
                string file = fi.FullName;
                if (!fi.Exists)
                {
                    continue;
                }

                try
                {
                    if (IsFileToBeDeleted(fi.Name))
                    {
                        long fsize = fi.Length;

                        if (!parser.DisplayOnly)
                        {
                            if (parser.UseRecycleBin)
                            {
                                fi.MoveToRecycleBin();
                            }
                            //FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                            else
                            {
                                try
                                {
                                    File.Delete(fi.FullName);
                                }
                                catch (PathTooLongException ex)
                                {
                                    // try again with long path routines
                                    fi.Delete();
                                }
                            }
                            catch (UnauthorizedAccessException ex)
                            {
                                fi.Attributes = ZetaLongPaths.Native.FileAttributes.Normal;
                                fi.Delete();
                            }
                        }
                        OnShowMessage(Resources.Deleting + file);
                        FileCount++;

                        FileSizeCount += fsize;
                    }
コード例 #5
0
        protected bool ProcessFolder(string activeFolder, DeleteFilesCommandLineParser parser)
        {
            ZetaLongPaths.ZlpFileInfo[] files;

            try
            {
                var folder = new ZetaLongPaths.ZlpDirectoryInfo(activeFolder);
                files = folder.GetFiles(parser.FileSpec);
            }
            catch (Exception e)
            {
                OnShowMessage(Resources.ErrorOpening + activeFolder + ". " + e.GetBaseException().Message);
                return false;
            }
            bool success = true;
            foreach (var fi in files)
            {
                //var fi = new FileInfo(file);
                string file = fi.FullName;
                if (!fi.Exists)
                    continue;

                try
                {
                    if (IsFileToBeDeleted(fi.FullName))
                    {
                        long fsize = fi.Length;

                        if (!parser.DisplayOnly)
                        {
                            if (parser.UseRecycleBin)
                                fi.MoveToRecycleBin();
                                //FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                            else
                                try
                                {
                                    File.Delete(fi.FullName);
                                }
                                catch (PathTooLongException ex)
                                {
                                    // try again with long path routines
                                    fi.Delete();
                                }
                                catch (UnauthorizedAccessException ex)
                                {
                                    fi.Attributes =  ZetaLongPaths.Native.FileAttributes.Normal;
                                    fi.Delete();
                                }
                        }
                        OnShowMessage(Resources.Deleting + file);
                        FileCount++;

                        FileSizeCount += fsize;
                    }
                }
                catch(Exception ex)
                {
                    OnShowMessage(Resources.FailedToDelete + file);
                    LockedFileCount++;
                    success = false;
                }
            }

            if (parser.Recursive)
            {
                var folders = new ZetaLongPaths.ZlpDirectoryInfo(activeFolder);
                var dirs = folders.GetDirectories();
                foreach (var directory in dirs)
                {
                    string dir = directory.FullName;

                    success = ProcessFolder(dir, parser);
                    if (success && parser.RemoveEmptyFolders)
                    {
                        if (!directory.GetFiles().Any() && !directory.GetDirectories().Any())
                            try
                            {
                                if (!parser.DisplayOnly)
                                {
                                    if (parser.UseRecycleBin)
                                        directory.MoveToRecycleBin();
                                        //FileSystem.DeleteDirectory(dir, UIOption.OnlyErrorDialogs,
                                        //    RecycleOption.SendToRecycleBin);
                                    else
                                    {
                                        try
                                        {
                                            Directory.Delete(directory.FullName);
                                        }
                                        catch (PathTooLongException ex)
                                        {
                                            directory.Delete(false);
                                        }
                                        catch (UnauthorizedAccessException ex)
                                        {
                                            if (parser.DeleteReadOnly)
                                            {
                                                directory.Attributes = ZetaLongPaths.Native.FileAttributes.Normal;
                                                directory.Delete(false);
                                            }
                                        }
                                    }
                                }
                                FolderCount++;
                                OnShowMessage(Resources.DeletingDirectory + dir);
                            }
                            catch
                            {
                                OnShowMessage(Resources.FailedToDeleteDirectory + dir);
                            }
                    }
                }
            }

            return success;
        }