/// <summary> /// Show folders and files hierarchically on the TreeView /// </summary> /// <param name="rootFolders"></param> public void SetFileHierarchies(FileHierarchies fileHierarchies) { SortedDictionary <string, Folder> _rootFolders = fileHierarchies.GetRootFolders(); if (_rootFolders == null) { return; } // remove all existing nodes in tree view _tvwFolders.Nodes.Clear(); foreach (var pair in _rootFolders) { Folder rootFolder = pair.Value; TreeNode rootNode = new TreeNode(rootFolder.Name); rootNode.Tag = rootFolder; // bind a folder information to the node SetCheckState(rootNode, rootFolder.CheckState); ConstructTreeRecursively(rootFolder, rootNode); _tvwFolders.Nodes.Add(rootNode); } }
/// <summary> /// Load the information of selected folders and files from a FileListExporter file /// </summary> /// <returns></returns> public FileHierarchies LoadFile() { BinaryFormatter formatter = new BinaryFormatter(); FileStream loadFile = System.IO.File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read); SortedDictionary <string, Folder> rootFolders = formatter.Deserialize(loadFile) as SortedDictionary <string, Folder>; FileHierarchies fileHierarchies = new FileHierarchies(rootFolders); loadFile.Close(); return(fileHierarchies); }
/// <summary> /// Load a txt file which is exported from SpaceSniffer program and returns file hierarchies /// </summary> /// <param name="txtFilePath">A full path of a txt file</param> /// <returns>True, if the file is successfully loaded</returns> public FileHierarchies LoadTxtFile(string txtFilePath) { FileHierarchies hierarchies = new FileHierarchies(); // open the file using (StreamReader reader = new StreamReader(txtFilePath)) { try { string line; Folder lastFolder = null; // process line by line while (!reader.EndOfStream) { // load one line line = reader.ReadLine(); // if the line is empty: if (line == null || line == "") { // ignore } // if the line is file information // (ex. " [ 67.4MB] AtestLeap.zip") else if (line.Length > 2 && line[0] == ' ' && line[1] == ' ') { // insert a file to the last folder if (lastFolder != null) { // split size and file name int sizeEndIndex = line.IndexOf(']'); string strSize = line.Substring(3, sizeEndIndex - 3); Size size = new Size(strSize); string fileName = line.Substring(sizeEndIndex + 2); // add a file to the last folder lastFolder.AddFile(fileName, size); } } // if the line is path information // (ex. "F:\BackUp\BackUp\Dcut [31.0GB]") else { // split path and size int sizeStartIndex = line.LastIndexOf(" ["); string path = line.Substring(0, sizeStartIndex); string strSize = line.Substring(sizeStartIndex + 2); strSize = strSize.Remove(strSize.LastIndexOf(']')); Size size = new Size(strSize); // add a path and ready to insert files into it lastFolder = hierarchies.AddPath(path, size); } } } // if an exception is occured while loading file catch (Exception e) { MessageBox.Show( text: "Failed to load \"" + txtFilePath + "\"!\nPlease make sure you select the correct file.\n\nException message: " + e.ToString(), caption: "", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); hierarchies = null; } finally { } } return(hierarchies); }
/// <summary> /// Initialize this program /// </summary> /// <param name="e"></param> protected override void OnShown(EventArgs e) { base.OnShown(e); EventHandlerManager.AddEventHandler(tvwFolders, "AfterCheck", this, "tvwFolders_AfterCheck"); EventHandlerManager.AddEventHandler(tvwFolders, "AfterSelect", this, "tvwFolders_AfterSelect"); EventHandlerManager.AddEventHandler(lvwFiles, "ItemChecked", this, "lvwFiles_ItemChecked"); EventHandlerManager.AddEventHandler(chkSelectAll, "CheckedChanged", this, "chkSelectAll_CheckedChanged"); tvwFoldersManager = new FolderTreeViewManager(tvwFolders); lvwFilesManager = new FileListViewManager(lvwFiles, colName, colType, colSize); // initialize the title message of the form _defaultMainFormText = Text; SetMessageToMainFormText(""); // initialize the status strip bar lblStatusStrip.Text = ""; // restrict file types that this app can open dlgOpenFile.Filter = "SpaceSniffer export file|*." + _txtExtention + "|FileListExporter File|*." + _fileListExtention; // show file open dialog DialogResult result = dlgOpenFile.ShowDialog(); // if a file is selected: if (result == DialogResult.OK) { fileListExporterFile.SetFilePath(dlgOpenFile.FileName); // if a txt file(SpaceSniffer export filer) is selected: if (fileListExporterFile.FileExtention == _txtExtention) { SetStatusLoading(dlgOpenFile.FileName); // try to load the selected txt file _fileHierarchies = spaceSnifferFile.LoadTxtFile(dlgOpenFile.FileName); // if the file hierarchies are successfully loaded: if (_fileHierarchies != null) { SetStatusLoaded(dlgOpenFile.FileName); // show FileListExporter file save dialog if (ShowSaveFileDialog(fileListExporterFile.FileDirectory, fileListExporterFile.FileNameBody)) { SetStatusSaving(fileListExporterFile.FilePath); fileListExporterFile.SaveFile(_fileHierarchies.GetRootFolders()); SetMessageToMainFormText(fileListExporterFile.FilePath); SetStatusSaved(fileListExporterFile.FilePath); SetStatusFoldersLoading(); EventHandlerManager.PauseAllEventHandlers(); { tvwFoldersManager.SetFileHierarchies(_fileHierarchies); } EventHandlerManager.ResumeAllEventHandlers(); SetStatusFoldersLoaded(); } // if a user gave up to proceed: else { // close the program Dispose(); } } else { // close the program Dispose(); } } // if a FileListExporter file is selected: else if (fileListExporterFile.FileExtention == _fileListExtention) { fileListExporterFile.SetFilePath(dlgOpenFile.FileName); SetStatusLoading(fileListExporterFile.FilePath); _fileHierarchies = fileListExporterFile.LoadFile(); // if the file hierarchies are successfully loaded: if (_fileHierarchies != null) { SetStatusLoaded(fileListExporterFile.FilePath); SetMessageToMainFormText(fileListExporterFile.FilePath); tvwFoldersManager.SetFileHierarchies(_fileHierarchies); } else { // close the program Dispose(); } } else { // close the program MessageBox.Show("Please select a file with these types only: " + dlgOpenFile.Filter); Dispose(); } } // if canceled: else { // close the app Dispose(); } }