public static Node Generate(System.IO.DirectoryInfo directory, bool recursive) { Node node = new Node(); foreach (var subdirectory in directory.EnumerateDirectories()) { Entry dirEntry = new Entry(subdirectory); node.Nodes.Add(dirEntry); if (recursive) { dirEntry.Node = Generate(subdirectory, recursive); dirEntry.UpdateSize(); } } foreach (var file in directory.EnumerateFiles()) { Entry fileEntry = new Entry(file, IsZipFile(file)); node.Nodes.Add(fileEntry); if ((fileEntry.Kind == EntryKind.ZipFile) && recursive) { fileEntry.Node = ZipFileGenerator.Generate(file); fileEntry.UpdateSize(); } } return node; }
public void ScanFolder(System.IO.DirectoryInfo directoryInfo, FolderToScan folderToScan, int? parentFolderId = null) { try { var folder = new Folder { Name = directoryInfo.Name, Path = directoryInfo.FullName.Replace(folderToScan.AbsolutePath, folderToScan.PathAlias), ParentFolderId = parentFolderId }; folderRepository.Add(folder); Logger.Trace("Added folder " + folder.Path); var fileInfos = directoryInfo.EnumerateFiles("*", System.IO.SearchOption.TopDirectoryOnly); var files = new List<Models.File>(); foreach (var fileInfo in fileInfos) { try { var file = new Models.File { Name = fileInfo.Name, Path = fileInfo.FullName.Replace(folderToScan.AbsolutePath, folderToScan.PathAlias), Size = fileInfo.Length, FolderId = folder.FolderId, CreatedTime = fileInfo.CreationTime, LastAccessTime = fileInfo.LastAccessTime, LastWriteTime = fileInfo.LastWriteTime }; files.Add(file); Logger.Trace("Added file " + file.Name); } catch (System.IO.PathTooLongException ex) { // TODO: Handle these (very common) errors } catch (Exception ex) { Logger.ErrorException("Error scanning file " + fileInfo.FullName, ex); } } fileRepository.Add(files); foreach (var subDirectory in directoryInfo.EnumerateDirectories("*", System.IO.SearchOption.TopDirectoryOnly)) { ScanFolder(subDirectory, folderToScan, parentFolderId = folder.FolderId); } } catch (System.IO.PathTooLongException ex) { // TODO: Handle these (very common) errors } catch (Exception ex) { Logger.ErrorException("Error scanning directory " + directoryInfo.FullName, ex); } }
public IEnumerable<System.IO.DirectoryInfo> EnumerateDirectories(System.IO.DirectoryInfo dir) { return dir.EnumerateDirectories(); }
public static void Update(System.IO.DirectoryInfo directory, Node node) { IDictionary<string, Entry> entries = new Dictionary<string, Entry>(node.Nodes.Count); foreach (var entry in node.Nodes) { entries.Add(entry.Name, entry); } foreach (var subdirectory in directory.EnumerateDirectories()) { if (entries.ContainsKey(subdirectory.Name)) { entries.Remove(subdirectory.Name); } else { Entry dirEntry = new Entry(subdirectory); node.Nodes.Add(dirEntry); } } foreach (var file in directory.EnumerateFiles()) { if (entries.ContainsKey(file.Name)) { Entry fileEntry = entries[file.Name]; entries.Remove(file.Name); if (fileEntry.LastWriteTime < file.LastWriteTime) { node.Nodes.Remove(fileEntry); node.Nodes.Add(new Entry(file, IsZipFile(file))); } } else { node.Nodes.Add(new Entry(file, IsZipFile(file))); } } foreach (var entryPair in entries) { node.Nodes.Remove(entryPair.Value); } }