public void Init() { //init here Height = 675; Width = 1200; menuDock.Visibility = Visibility.Collapsed; rootPath = new FileItemView { SubItems = new ObservableCollection <FileItemView> { new FileItemView { Name = "", DisplayName = "Root", Icon = new BitmapImage(new Uri(@"Resources/Images/Computer16.png", UriKind.RelativeOrAbsolute)), } } }; CurrentPath = rootPath; SetBindings(); }
private void CreateFileItemViews() { string[] dirs = Directory.GetDirectories(ExploringDir).Where((string dirName) => { return(!ExcludeDirNames.Contains(dirName)); }).ToArray(); string[] files = Directory.GetFiles(ExploringDir).Where((string fileName) => { if (!fileName.Contains('.')) { return(true); } return(!ExcludeFileExtensions.Contains(Path.GetExtension(fileName))); }).ToArray(); foreach (string dir in dirs) { FileItemView itemView = new FileItemView(dir, FileIconType.Directory); itemView.RegisterDoubleClickEvent(() => { ExploreDir(itemView.FullFilename); }); FileItemContext.Children.Add(itemView); } foreach (string file in files) { FileItemView itemView = new FileItemView(file, FileIconType.File); FileItemContext.Children.Add(itemView); } }
private async Task ShowFileDetailsDialog(FileItemView fv) { if (string.IsNullOrEmpty(fv.SHA256) || string.IsNullOrEmpty(fv.PreBytes)) { try { using FileStream fs = fv.FileInfo.Open(FileMode.Open); if (string.IsNullOrEmpty(fv.PreBytes)) { fs.Position = 0; byte[] prebs = new byte[4]; await fs.ReadAsync(prebs, 0, 4); fv.PreBytes = FilesHashComputer.ToHexString(prebs); } if (string.IsNullOrEmpty(fv.SHA256)) { fs.Position = 0; using (SHA256 sha256 = SHA256.Create()) { fv.SHA256 = "Sha256正在计算中..."; byte[] hv = await sha256.ComputeHashAsync(fs); fv.SHA256 = FilesHashComputer.ToHexString(hv); fv.IsOK = true; } } fs.Close(); } catch (UnauthorizedAccessException unex) { fv.SHA256 = "未授权访问"; ShowErrorMsg(unex.Message); } catch (Exception ex) { ShowErrorMsg(ex.Message); } } var fdd = new FileDetailsDialog(fv); fdd.ShowDialog(); }
private async void BtnSearch_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(filesView.FolderName)) { return; } if (!Directory.Exists(filesView.FolderName)) { return; } try { filesView.FileHashComputed = false; DirectoryInfo dirInfo = new DirectoryInfo(filesView.FolderName); FileInfo[] files = dirInfo.GetFiles("", SearchOption.AllDirectories); if (!files.Any()) { return; } filesView.Total = files.Length; filesView.FileItems.Clear(); List <FileItemView> fis = new List <FileItemView>(); foreach (var f in files) { string rn = System.IO.Path.GetRelativePath(filesView.FolderName, f.FullName); FileItemView fi = new FileItemView() { FileInfo = f, Name = rn, FullName = f.FullName, Length = f.Length, LastWriteTime = f.LastWriteTime, CreationTime = f.CreationTime, }; fis.Add(fi); filesView.FileItems.Add(fi); } List <FileInfo> hashFiles = new List <FileInfo>(); var lenGroup = fis.GroupBy(x => x.Length); foreach (var g in lenGroup) { if (g.Count() >= 2) { hashFiles.AddRange(g.Select(x => x.FileInfo)); } } int hashCount = hashFiles.Count; filesView.Count = files.Length - hashCount; if (hashCount >= 1) { FilesHashComputer computer = new FilesHashComputer(hashFiles); computer.FileHashComputed += Computer_FileHashComputed; computer.FileHashError += Computer_FileHashError; await computer.ComputeHash(); filesView.FileHashComputed = true; } } catch (Exception ex) { PrintError(ex); } }
/// <summary> /// 构造函数 /// </summary> public FileDetailsDialog(FileItemView fv) { InitializeComponent(); DataContext = fv; }