FileRestoreForm.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Oreo.FileMan.Commons;
  2. using Oreo.FileMan.DatabaseEngine;
  3. using Oreo.FileMan.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Y.Skin.YoForm.NoTitle;
  15. using Y.Utils.DataUtils.Collections;
  16. using Y.Utils.DataUtils.UnitConvertUtils;
  17. using Y.Utils.IOUtils.PathUtils;
  18. namespace Oreo.FileMan.Views
  19. {
  20. public partial class FileRestoreForm : NoTitleForm
  21. {
  22. public string FilePath { get; set; }
  23. public List<BackupFiles> Files { get; set; }
  24. public FileRestoreForm(string path)
  25. {
  26. InitializeComponent();
  27. FilePath = path;
  28. }
  29. private void FileRestoreForm_Load(object sender, EventArgs e)
  30. {
  31. //读取该文件的备份记录
  32. using (var db = new Muse())
  33. {
  34. Files = db.Gets<BackupFiles>(x => x.FullPath == FilePath, null).OrderBy(x => x.Id).ToList();
  35. }
  36. if (ListTool.HasElements(Files))
  37. {
  38. //获取文件名及路径信息
  39. var first = Files.FirstOrDefault();
  40. LbFileName.Text = Path.GetFileName(first.FullPath);
  41. LbPath.Text = first.FullPath;
  42. LbVersion.Text = "共 " + Files.Count + " 版";
  43. TtLabel.SetToolTip(LbFileName, Path.GetFileName(first.FullPath));
  44. TtLabel.SetToolTip(LbPath, first.FullPath);
  45. //显示所有备份记录
  46. int index = 1;
  47. foreach (var file in Files)
  48. {
  49. DgvFiles.Rows.Add(string.Format("第 {0} 版", index++), file.LastWriteTime, ByteConvertTool.Fmt(file.Size));
  50. }
  51. }
  52. }
  53. private void BtClose_Click(object sender, EventArgs e)
  54. {
  55. Close();
  56. }
  57. private void BtRestoreToNew_Click(object sender, EventArgs e)
  58. {
  59. if (DgvFiles.CurrentRow != null && DgvFiles.CurrentRow.Index >= 0)
  60. {
  61. BackupFiles file = Files[DgvFiles.CurrentRow.Index];
  62. SaveFileDialog sfd = new SaveFileDialog();
  63. sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//设置默认目录
  64. sfd.FileName = Path.GetFileName(file.FullPath);//设置默认文件名
  65. sfd.Filter = "还原文件|*" + Path.GetExtension(file.FullPath);//设置默认文件类型
  66. if (sfd.ShowDialog() == DialogResult.OK)
  67. {
  68. string from = file.BackupFullPath;
  69. string to = sfd.FileName;
  70. File.Copy(from, to, true);
  71. }
  72. }
  73. }
  74. private void BtRestoreToOld_Click(object sender, EventArgs e)
  75. {
  76. if (DgvFiles.CurrentRow != null && DgvFiles.CurrentRow.Index >= 0)
  77. {
  78. BackupFiles file = Files[DgvFiles.CurrentRow.Index];
  79. string title = string.Format("文件还原", file.LastWriteTime);
  80. string text = string.Format("您确定将文件:{0} [ {1} ]{2}还原到:{2}{3} 吗?", Path.GetFileName(file.FullPath), file.LastWriteTime, Environment.NewLine, file.FullPath);
  81. if (MessageBox.Show(text, title, MessageBoxButtons.OKCancel) == DialogResult.OK)
  82. {
  83. string from = file.BackupFullPath;
  84. string to = file.FullPath;
  85. string topath = DirTool.GetFilePath(to);
  86. if (DirTool.Create(topath))
  87. {
  88. File.Copy(from, to, true);
  89. }
  90. else
  91. {
  92. MessageBox.Show(string.Format("路径:{0} 不存在,请还原到其他路径。", topath), "路径不存在");
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }