fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12.  
  13.  
  14.  
  15.  
  16. namespace WindowsFormsApplication1
  17. {
  18. public partial class Form1 : Form
  19. {
  20. DriveInfo[] dis = DriveInfo.GetDrives();
  21. List<Class1> filesinfo = new List<Class1>();
  22. List<long> filessize = new List<long>();
  23. List<Class1> filesnames = new List<Class1>();
  24.  
  25. public void FindInDir(DirectoryInfo dir, string pattern) // поиск файлов в папке, проблема с закрытым доступом к папкам
  26. {
  27. foreach (FileInfo file in dir.GetFiles(pattern))
  28. {
  29. this.listView1.Items.Add(file.FullName);
  30. filesinfo.Add(new Class1() { FileName = file.Name, FileFullName =file.FullName, FileSize = file.Length });
  31. }
  32. foreach (DirectoryInfo subdir in dir.GetDirectories())
  33. {
  34. this.FindInDir(subdir, pattern);
  35. }
  36. }
  37.  
  38. public Form1()
  39. {
  40. InitializeComponent();
  41. }
  42. private void button1_Click(object sender, EventArgs e)
  43. {
  44. filesinfo.Clear(); //очистка списков перед работой
  45. filessize.Clear();
  46. filesnames.Clear();
  47. try
  48. {
  49. foreach (DriveInfo di in dis)
  50. {
  51. if (di.IsReady)
  52. {
  53. DirectoryInfo dir = new DirectoryInfo(di.Name/*"H:\\"*/);
  54. string file = "*.*";
  55. richTextBox1.AppendText(" Диск " + di.Name + Environment.NewLine);
  56. FindInDir(dir, file);
  57. }
  58. }
  59. }
  60. catch (UnauthorizedAccessException)
  61. {
  62. throw;
  63. }
  64. catch (IOException ex)
  65. {
  66. MessageBox.Show((ex.GetType().Name));
  67. }
  68.  
  69.  
  70. var FileSGroup = filesinfo.GroupBy(p1 => p1.FileSize) // группировка файлов по размеру
  71. .Where(p1 => p1.Count() > 1) // фильтр не дублей
  72. .Select(g => new { FileSize = g.Key, Count = g.Count() });
  73.  
  74. foreach (var group in FileSGroup) // запись значений размера дублей во второй список
  75. {
  76. filessize.Add(group.FileSize);
  77. }
  78.  
  79. foreach (Class1 cl in filesinfo) // сравнение первичного списка файлов по размеру с дубликатами и запись подходящих в третий список
  80. {
  81. for (int i = 0; i < filessize.Count; i++)
  82. {
  83. if (cl.FileSize == filessize[i] )
  84. {
  85. filesnames.Add(new Class1() { FileName = cl.FileName, FileFullName = cl.FileFullName, FileSize = cl.FileSize });
  86. }
  87. }
  88. }
  89.  
  90. var SortedFiles = from f in filesnames // сортировка по размеру
  91. orderby f.FileSize descending
  92. select f;
  93.  
  94. foreach (Class1 cl in SortedFiles) // вывод списка в текстбокс. Расчет хеша надо сделать после группировки дублей
  95. {
  96. richTextBox1.AppendText("Имя файла: " + cl.FileFullName + "Размер файла: " + cl.FileSize + " хеш = " + GetMD5HashFromFile(cl.FileFullName)+ Environment.NewLine);
  97. }
  98. }
  99.  
  100. class Class1
  101. {
  102. public string FileName { get; set; }
  103. public string FileFullName { get; set; }
  104. public long FileSize { get; set; }
  105. public override string ToString()
  106. {
  107. return " Name: " + FileName + " Size: " + FileSize;
  108. }
  109. }
  110.  
  111. public string GetMD5HashFromFile(string fileName) // подсчет хеша, стянул блоком
  112. {
  113. FileStream file = new FileStream(fileName, FileMode.Open);
  114. MD5 md5 = new MD5CryptoServiceProvider();
  115. byte[] retVal = md5.ComputeHash(file);
  116. file.Close();
  117.  
  118. StringBuilder sb = new StringBuilder();
  119. for (int i = 0; i < retVal.Length; i++)
  120. {
  121. sb.Append(retVal[i].ToString("x2"));
  122. }
  123. return sb.ToString();
  124. }
  125.  
  126.  
  127.  
  128. private void button2_Click(object sender, EventArgs e)
  129. {
  130. richTextBox1.Text = "";
  131. richTextBox1.Clear();
  132. listView1.Text = "";
  133. listView1.Clear();
  134. }
  135.  
  136. }
  137. }
  138.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(4,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing `System.Data' assembly reference?
prog.cs(9,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
prog.cs(18,34): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
Compilation failed: 3 error(s), 0 warnings
stdout
Standard output is empty