private ProofFileFolder GetFolder(string folderPath, ref int index, int parentIndex = -1) { ProofFileFolder proofFileFolder = new ProofFileFolder(); proofFileFolder.Name = folderPath.Replace(System.IO.Path.GetDirectoryName(folderPath), string.Empty).Substring(1); proofFileFolder.ParentIndex = parentIndex; proofFileFolder.Index = index++; proofFileFolderArr.Add(proofFileFolder); foreach (string file in Directory.GetFiles(folderPath)) { ProofFile proofFile = new ProofFile(); proofFile.Name = System.IO.Path.GetFileNameWithoutExtension(file); proofFile.Path = file; switch (System.IO.Path.GetExtension(file).ToLower()) { case ".png": case ".jpg": case ".jpeg": case ".bmp": case ".gif": proofFile.FileType = FileTypeEnum.Img; break; case ".doc": case ".docx": proofFile.FileType = FileTypeEnum.Word; break; case "xls": case "xlsx": proofFile.FileType = FileTypeEnum.Excel; break; case ".pdf": proofFile.FileType = FileTypeEnum.Pdf; break; default: throw new Exception("未知文件格式"); } proofFile.Length = File.ReadAllBytes(file).Length; proofFileFolder.Files.Add(proofFile); } foreach (string folder in Directory.GetDirectories(folderPath)) { proofFileFolder.Folders.Add(GetFolder(folder, ref index, proofFileFolder.Index)); } return(proofFileFolder); }
private void OpenFolderDialogExecuted(object sender, ExecutedRoutedEventArgs e) { proofFileFolderArr.Clear(); try { using (System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog()) { fbd.ShowNewFolderButton = false; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { int index = 0; proofFileFolder = GetFolder(fbd.SelectedPath, ref index); } } } catch { MessageBox.Show("读取失败。"); } }
private IList <ProofItem> ConvertToProofItems(ProofFileFolder root, ProofItem parent = null) { IList <ProofItem> proofs = new List <ProofItem>(); for (int i = 0; i < root.Folders.Count; i++) { ProofItem proof = new ProofItem(); proof.Type = FileTypeEnum.Directory; proof.Name = root.Folders[i].Name; proof.ImgSource = GetImageSource(proof); proof.Proofs = new ObservableCollection <ProofItem>(); proof.Parent = parent; IList <ProofItem> children = ConvertToProofItems(root.Folders[i], proof); for (int j = 0; j < children.Count; j++) { proof.Proofs.Add(children[j]); } proofs.Add(proof); } for (int i = 0; i < root.Files.Count; i++) { ProofItem proof = new ProofItem(); proof.Type = root.Files[i].FileType; proof.Name = root.Files[i].Name; proof.StartIndex = root.Files[i].StartIndex; proof.Length = root.Files[i].Length; proof.ImgSource = GetImageSource(proof); proofs.Add(proof); } return(proofs); }
private ProofFileFolder GetProofFileFolderRoot() { Dictionary <int, ProofFileFolder> folders = new Dictionary <int, ProofFileFolder>(); IList <ProofFileFolder> _folders = new List <ProofFileFolder>(); FileStream idxFs = null; byte[] buffer = null; int[] folderFileCountArray = null; int[] folderNameLengthArray = null; int[][] fileNameLengthArray = null; try { idxFs = new FileStream(proofIndexFileName, FileMode.Open, FileAccess.Read); buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); int folderCount = BitConverter.ToInt32(buffer, 0); folderFileCountArray = new int[folderCount]; folderNameLengthArray = new int[folderCount]; fileNameLengthArray = new int[folderCount][]; for (int i = 0; i < folderFileCountArray.Length; i++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); folderFileCountArray[i] = BitConverter.ToInt32(buffer, 0); ProofFileFolder folder = new ProofFileFolder(); _folders.Add(folder); for (int j = 0; j < folderFileCountArray[i]; j++) { ProofFile proofFile = new ProofFile(); folder.Files.Add(proofFile); } } for (int i = 0; i < folderFileCountArray.Length; i++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Index = BitConverter.ToInt32(buffer, 0); folders.Add(_folders[i].Index, _folders[i]); } for (int i = 0; i < folderFileCountArray.Length; i++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].ParentIndex = BitConverter.ToInt32(buffer, 0); } for (int i = 0; i < folderCount; i++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); folderNameLengthArray[i] = BitConverter.ToInt32(buffer, 0); } for (int i = 0; i < folderFileCountArray.Length; i++) { fileNameLengthArray[i] = new int[folderFileCountArray[i]]; for (int j = 0; j < folderFileCountArray[i]; j++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); fileNameLengthArray[i][j] = BitConverter.ToInt32(buffer, 0); } } for (int i = 0; i < folderFileCountArray.Length; i++) { buffer = new byte[folderNameLengthArray[i]]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Name = UTF8Encoding.UTF8.GetString(buffer); } for (int i = 0; i < folderFileCountArray.Length; i++) { for (int j = 0; j < folderFileCountArray[i]; j++) { buffer = new byte[fileNameLengthArray[i][j]]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Files[j].Name = UTF8Encoding.UTF8.GetString(buffer); } } for (int i = 0; i < folderFileCountArray.Length; i++) { for (int j = 0; j < folderFileCountArray[i]; j++) { buffer = new byte[sizeof(int)]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Files[j].FileType = (FileTypeEnum)BitConverter.ToInt32(buffer, 0); } } for (int i = 0; i < folderFileCountArray.Length; i++) { for (int j = 0; j < folderFileCountArray[i]; j++) { buffer = new byte[sizeof(long)]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Files[j].StartIndex = BitConverter.ToInt64(buffer, 0); buffer = new byte[sizeof(long)]; idxFs.Read(buffer, 0, buffer.Length); _folders[i].Files[j].Length = BitConverter.ToInt64(buffer, 0); } } for (int i = 0; i < folderFileCountArray.Length; i++) { if (_folders[i].ParentIndex == -1) { continue; } folders[_folders[i].ParentIndex].Folders.Add(_folders[i]); } return(folders[0]); } finally { if (idxFs != null) { idxFs.Dispose(); } } }