private void listView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { const int bufferSize = 4096; var item = (DirectoryItem)listView.SelectedItem; if (item == null) { return; } if (item.isDirectory) { if (item.name == ".") { Reload(); return; } if (item.name == "..") { GoUp(); return; } if (history.Count - historyNeedle - 1 > 0) { history.RemoveRange(historyNeedle + 1, history.Count - historyNeedle - 1); } var newDir = currentDirectory + item.name + "/"; if (LoadDirectory(newDir)) { history.Add(newDir); historyNeedle++; buttonForward.IsEnabled = (historyNeedle + 1 < history.Count); buttonBack.IsEnabled = (historyNeedle - 1 >= 0); } } else { // 读取文件内容到临时变量 String tempFileName = System.IO.Path.GetTempFileName() + System.IO.Path.GetExtension(item.path); using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(new System.IO.FileStream(tempFileName, System.IO.FileMode.Create))) { var file = vfs.NewFile(item.path, VFS.FileMode.Open); byte[] buffer = new byte[bufferSize]; int count; while ((count = (int)file.Read(buffer, 0, (uint)buffer.Length)) != 0) { writer.Write(buffer, 0, count); } } FadeOutWindow(); XamDialogWindow win = CreateDialogWindow(); win.Header = "VFS"; Label label = new Label(); label.Content = "正在等待应用程序关闭, 文件内容将在程序关闭后自动更新..."; label.VerticalContentAlignment = VerticalAlignment.Center; label.HorizontalAlignment = HorizontalAlignment.Center; win.Content = label; win.IsModal = true; windowContainer.Children.Add(win); Utils.ProcessUITasks(); // 调用系统默认程序打开 var process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo(tempFileName); process.EnableRaisingEvents = true; process.Start(); try { process.WaitForExit(); } catch (Exception ex) { } // 在关闭后,读取内容写回文件系统 using (System.IO.BinaryReader reader = new System.IO.BinaryReader(new System.IO.FileStream(tempFileName, System.IO.FileMode.Open))) { var file = vfs.NewFile(item.path, VFS.FileMode.Create); byte[] buffer = new byte[bufferSize]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { file.Write(buffer, 0, (uint)count); } } win.Close(); windowContainer.Children.Remove(win); FadeInWindow(); Reload(); UpdateInfo(); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { if (Properties.Settings.Default["vfsfile"] == null || ((String)(Properties.Settings.Default["vfsfile"])).Length == 0) { var r = System.Windows.Forms.MessageBox.Show("首次启动 VFS 需要在磁盘上建立一个虚拟磁盘镜像文件,镜像文件大小为 1GB。\n点击确定后请选择一个可以写入的位置来建立此文件,点击取消关闭程序。", "磁盘镜像文件未找到", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Information); if (r == System.Windows.Forms.DialogResult.Cancel) { Close(); return; } using (var dialog = new System.Windows.Forms.SaveFileDialog()) { dialog.Title = "保存磁盘镜像文件"; dialog.FileName = "vfs.bin"; var result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { Close(); return; } else { try { device = new FileAdapter(dialog.FileName, 1 << 10 << 10 << 10); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("打开镜像文件失败,可能您没有权限在该位置写入文件,或您内存不足。程序即将退出,请重新打开程序。"); Close(); return; } // 更新配置 Properties.Settings.Default["vfsfile"] = dialog.FileName; Properties.Settings.Default.Save(); } } } else { device = new FileAdapter((String)Properties.Settings.Default["vfsfile"], 1 << 10 << 10 << 10); } vfs = new VFS(device); if (!vfs.IsFormated()) { var result = System.Windows.Forms.MessageBox.Show("该磁盘镜像文件尚未格式化,是否格式化?", "VFS", System.Windows.Forms.MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.No) { Close(); return; } vfs.Format(device.Size() >> 10, 4); // 写入一个示例文件 var file = vfs.NewFile("/README.txt", VFS.FileMode.Create); file.Write(Encoding.UTF8.GetBytes("Hello world!\r\n\r\n这个文件是格式化时自动建立的,如果你看到了这个文件,说明一切工作正常。\r\n\r\n当你使用记事本修改这个文件并保存以后,它会同步到虚拟文件系统中。\r\n\r\n该虚拟文件系统是索引文件系统,inode 默认占总空间的 10%,理论可支持单一文件最大 2GB。")); System.Windows.Forms.MessageBox.Show("磁盘格式化成功!", "VFS", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } LoadDirectory("/"); UpdateInfo(); }