public static DirectoryInformation FindRecursive(string path) { var root = new DirectoryInformation { FullPath = Path.GetFullPath(path), Name = Path.GetDirectoryName(path) ?? Path.GetFullPath(path) }; List<FileInformation> fileList; List<DirectoryInformation> directoryList; path = path.TrimEnd('\\'); FindRecursive(path, root, out fileList, out directoryList); root.DescendantFiles.AddRange(fileList); root.DescendantFolders.AddRange(directoryList); return root; }
private static void ImportFiles(SQLiteConnection connection, DirectoryInformation rootFolder, IDictionary<string, long> folderMap) { using (var cmd = new SQLiteCommand(connection)) { cmd.CommandText = "insert into Files (Name, FolderID, Size, CreateDate, LastWriteDate) VALUES (@name, @folder, @size, @createDate, @lastWriteDate);"; cmd.Parameters.Add("@name", DbType.String); cmd.Parameters.Add("@folder", DbType.Int64); cmd.Parameters.Add("@size", DbType.UInt64); cmd.Parameters.Add("@createDate", DbType.DateTime); cmd.Parameters.Add("@lastWriteDate", DbType.DateTime); using (var tx = connection.BeginTransaction()) { foreach (var file in rootFolder.DescendantFiles) { var parentId = GetFolderParent(file, folderMap); CreateFile(cmd, file, parentId); } tx.Commit(); } } }
private static bool FindRecursive(string path, DirectoryInformation parent, out List<FileInformation> files, out List<DirectoryInformation> directories) { List<FileInformation> fileList = new List<FileInformation>(); List<DirectoryInformation> directoryList = new List<DirectoryInformation>(); IntPtr findHandle = INVALID_HANDLE_VALUE; try { WIN32_FIND_DATAW findData; findHandle = FindFirstFileW(path + @"\*", out findData); if (findHandle != INVALID_HANDLE_VALUE) { do { if (findData.cFileName == "." || findData.cFileName == "..") continue; string fullPath = path + @"\" + findData.cFileName; if (findData.dwFileAttributes.HasFlag(FileAttributes.System)) continue; if (findData.dwFileAttributes.HasFlag(FileAttributes.Hidden)) continue; if (findData.dwFileAttributes.HasFlag(FileAttributes.Directory) && !findData.dwFileAttributes.HasFlag(FileAttributes.ReparsePoint)) { var subFolder = findData.ToDirectoryInformation(fullPath); subFolder.Directory = parent; parent.Folders.Add(subFolder); directoryList.Add(subFolder); List<FileInformation> subDirectoryFileList; List<DirectoryInformation> subDirectoryDirectoryList; if (FindRecursive(fullPath, subFolder, out subDirectoryFileList, out subDirectoryDirectoryList)) { parent.DescendantFileSize += subFolder.DescendantFileSize; fileList.AddRange(subDirectoryFileList); directoryList.AddRange(subDirectoryDirectoryList); subFolder.DescendantFiles.AddRange(subDirectoryFileList); subFolder.DescendantFolders.AddRange(subDirectoryDirectoryList); } } else if (!findData.dwFileAttributes.HasFlag(FileAttributes.Directory)) { var fileInformation = findData.ToFileInformation(fullPath); fileInformation.Directory = parent; parent.Files.Add(fileInformation); fileList.Add(fileInformation); parent.FileSize += fileInformation.FileSize; parent.DescendantFileSize += fileInformation.FileSize; } } while (FindNextFile(findHandle, out findData)); } } catch (Exception exception) { Console.WriteLine("Caught exception while trying to enumerate a directory. {0}", exception); if (findHandle != INVALID_HANDLE_VALUE) FindClose(findHandle); files = null; directories = null; return false; } if (findHandle != INVALID_HANDLE_VALUE) FindClose(findHandle); files = fileList; directories = directoryList; return true; }
private static IDictionary<string, long> ImportFolders(SQLiteConnection connection, DirectoryInformation rootFolder) { IDictionary<string, long> folderMap = new Dictionary<string, long>(); using (var cmd = new SQLiteCommand(connection)) { cmd.CommandText = "insert into Folders (Name, ParentID) VALUES (@name, @parent);"; cmd.Parameters.Add("@name", DbType.String); cmd.Parameters.Add("@parent", DbType.Int64); using (var tx = connection.BeginTransaction()) { var id = CreateFolder(cmd, rootFolder.Name, null); folderMap.Add(rootFolder.FullPath, id); foreach (var folder in rootFolder.DescendantFolders) { var parentId = GetFolderParent(folder, folderMap); var folderId = CreateFolder(cmd, folder.Name, parentId); folderMap.Add(folder.FullPath, folderId); } tx.Commit(); } } return folderMap; }