예제 #1
0
        public static NumberOfFiles GetFilesNumber(DirectoryInfo directory, NumberOfFiles numberOfFiles)
        {
            var files = directory.GetFiles();

            if (files.Length > 0)
            {
                numberOfFiles.SmallSize  += files.Count(f => f.Length <= 10485760);
                numberOfFiles.MiddleSize += files.Count(f => f.Length > 10485760 && f.Length <= 104857600);
                numberOfFiles.LargeSize  += files.Count(f => f.Length > 104857600);
            }
            var subDirectories = directory.GetDirectories();

            if (subDirectories.Length > 0)
            {
                foreach (var subdir in subDirectories)
                {
                    try
                    {
                        GetFilesNumber(subdir, numberOfFiles);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        continue;
                    }
                }
            }
            return(numberOfFiles);
        }
예제 #2
0
        public static Folder BuildTree(FileSystemContext db, Guid id)
        {
            List <FileSystemItem> items = new List <FileSystemItem>();
            Folder folder;

            if (id == Guid.Empty)
            {
                IEnumerable <DriveInfo> drives        = DriveInfo.GetDrives().ToList().Where(d => d.IsReady);
                NumberOfFiles           numberOfFiles = new NumberOfFiles();
                foreach (var driver in drives)
                {
                    FileSystemItem fileSystemItem = new FileSystemItem
                    {
                        Id                 = Guid.NewGuid(),
                        FolderId           = id,
                        Name               = driver.RootDirectory.FullName,
                        Path               = driver.RootDirectory.FullName,
                        FileSystemItemType = FileSystemItemType.Folder
                    };
                    items.Add(fileSystemItem);
                    var number = GetFilesNumber(driver.RootDirectory, new NumberOfFiles());
                    numberOfFiles.SmallSize  += number.SmallSize;
                    numberOfFiles.MiddleSize += number.MiddleSize;
                    numberOfFiles.LargeSize  += number.LargeSize;
                }
                folder = new Folder
                {
                    Id              = Guid.NewGuid(),
                    ParentId        = id,
                    FileSystemItems = items,
                    FullName        = "My Computer",
                    NumberOfFiles   = numberOfFiles
                };
                db.Folders.Add(folder);
                db.SaveChanges();
                return(folder);
            }
            FileSystemItem item      = db.FileSystemItems.Find(id);
            DirectoryInfo  directory = new DirectoryInfo(item.Path);

            try
            {
                var subDirectories = directory.GetDirectories();
                if (subDirectories.Count() > 0)
                {
                    foreach (var dir in subDirectories)
                    {
                        FileSystemItem subDir = new FileSystemItem
                        {
                            Id                 = Guid.NewGuid(),
                            FolderId           = id,
                            Name               = dir.Name,
                            Path               = dir.FullName,
                            FileSystemItemType = FileSystemItemType.Folder
                        };
                        items.Add(subDir);
                    }
                }
                var files = directory.GetFiles();
                if (files.Count() > 0)
                {
                    foreach (var file in files)
                    {
                        FileSystemItem fsi = new FileSystemItem
                        {
                            Id                 = Guid.NewGuid(),
                            FolderId           = id,
                            Name               = file.Name,
                            Path               = file.DirectoryName,
                            FileSystemItemType = FileSystemItemType.File
                        };
                        items.Add(fsi);
                    }
                }
                folder = new Folder
                {
                    Id              = id,
                    ParentId        = item.FolderId,
                    FileSystemItems = items,
                    FullName        = directory.FullName,
                    NumberOfFiles   = GetFilesNumber(directory, new NumberOfFiles())
                };
            }
            catch (UnauthorizedAccessException)
            {
                folder = new Folder
                {
                    Id              = id,
                    ParentId        = item.FolderId,
                    FileSystemItems = items,
                    FullName        = "Unauthorized",
                    NumberOfFiles   = new NumberOfFiles()
                };
            }
            db.Folders.Add(folder);
            db.SaveChanges();
            return(folder);
        }