Advertisement
Guest User

Check size

a guest
Dec 8th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  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 Excel = Microsoft.Office.Interop.Excel;
  12. using System.Collections;
  13. using System.Threading;
  14. using System.Diagnostics;
  15.  
  16.  
  17. namespace CheckSize
  18. {
  19. public partial class CheckSize : Form
  20. {
  21. int itemToList = 0;
  22. double totalSize;
  23. static double TotalSizeOfFolderByte;
  24. static double TotalSizeOfFolder;
  25. static double sizeOfDirKb;
  26. static string[] arr = new string[30];
  27. int countListviewShowFolder = 0;
  28. int counterNoListViewFolder = 1;
  29. int counterColumnListPathSize = 80;
  30. string[] GetPathFromlistViewShowFolder;
  31. string[] GetFullPath;
  32. double[] StoreSizeofFolder =new double[30];
  33. int CounterPositionofList=0;
  34. int[] storeCounterPositionToArray=new int[30];
  35. ArrayList eeee = new ArrayList();
  36. string[] LocationGet;
  37.  
  38. double storeTotalSize = 0;
  39.  
  40. ArrayList storeNamePath = new ArrayList();
  41. ArrayList storeLastNamePath = new ArrayList();
  42. string TheLastFolderName=null;
  43.  
  44. int countAllArrayList = 0;
  45.  
  46.  
  47. ArrayList StoreLocation = new ArrayList();
  48. ArrayList storeLocationcheckSize = new ArrayList();
  49.  
  50. string[] storeAllLocation = new string[1000];
  51.  
  52.  
  53. ArrayList GetAllLocation = new ArrayList();
  54.  
  55. public CheckSize()
  56. {
  57.  
  58.  
  59. InitializeComponent();
  60.  
  61. }
  62.  
  63. public void getSize(string pathLocation)
  64. {
  65. try
  66. {
  67. DirectoryInfo dInfo = new DirectoryInfo(@pathLocation);
  68. sizeOfDirKb = DirectorySize(dInfo, true) / (1024);
  69.  
  70.  
  71. TotalSizeOfFolderByte = sizeOfDirKb / (1024 * 1024);
  72.  
  73. if (TotalSizeOfFolderByte < 100)
  74. {
  75. TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte * 100) / 100;
  76.  
  77. }
  78. else if (TotalSizeOfFolderByte >= 100)
  79. {
  80. TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte);
  81.  
  82.  
  83. }
  84. itemToList++;
  85. }
  86. catch (Exception e)
  87. {
  88.  
  89. }
  90. // itemToList++;
  91. }
  92. double Total=0;
  93. double total1 = 0;
  94. double DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
  95. {
  96.  
  97. Application.DoEvents();
  98.  
  99. totalSize = dInfo.EnumerateFiles()
  100. .Sum(file => file.Length);
  101. Total = Total + totalSize / 1024 / 1024 / 1024;
  102. total1 = Total;
  103. if (total1 < 100)
  104. {
  105. total1 = Math.Floor(total1 * 100) / 100;
  106.  
  107. }
  108. else if (total1 >= 100)
  109. {
  110. total1 = Math.Floor(total1);
  111.  
  112.  
  113. }
  114. if (includeSubDir)
  115. {
  116. totalSize += dInfo.EnumerateDirectories()
  117. .Sum(dir => DirectorySize(dir, true));
  118.  
  119.  
  120. }
  121.  
  122. // Total = totalSize / (1024 * 1024);
  123.  
  124.  
  125.  
  126. //Console.WriteLine(Total);
  127. listViewShowPathSize.Items[itemToList].SubItems[4].Text = (total1).ToString();
  128. listViewShowPathSize.EnsureVisible(listViewShowPathSize.Items.Count - 1);
  129.  
  130. return totalSize;
  131.  
  132. }
  133.  
  134.  
  135.  
  136. private void btnChooseFile_Click(object sender, EventArgs e)
  137. {
  138. listViewShowFolder.Items.Clear();
  139. listViewShowPathSize.Items.Clear();
  140. btnGetFolder.Enabled = true;
  141. btnStart.Enabled = true;
  142. countListviewShowFolder = 0;
  143. counterNoListViewFolder = 1;
  144. OpenFileDialog ChooseFile = new OpenFileDialog();
  145. DialogResult ResultPathFile = ChooseFile.ShowDialog();
  146. if (ResultPathFile == DialogResult.OK)
  147. {
  148. txtPathFile.Clear();
  149. txtPathFile.Text = ChooseFile.FileName;
  150. }
  151.  
  152.  
  153. }
  154.  
  155. public void readLocationFromFile(string Location)
  156. {
  157.  
  158. LocationGet = File.ReadAllLines(Location);
  159.  
  160. foreach (string a in LocationGet)
  161. {
  162. StoreLocation.Add(a);
  163.  
  164. foreach (string b in Directory.GetDirectories(a, "*", SearchOption.TopDirectoryOnly))
  165. {
  166. GetAllLocation.Add(b);
  167. }
  168. }
  169.  
  170. }
  171.  
  172.  
  173. public void CheckBoxListFolder()
  174. {
  175.  
  176. for (int i = 0; i < listViewShowFolder.Items.Count; i++)
  177. {
  178. if (listViewShowFolder.Items[i].Checked == true)
  179. {
  180. GetPathFromlistViewShowFolder = listViewShowFolder.Items[i].Text.Split('\\');
  181.  
  182. listViewShowPathSize.Columns.Add(GetPathFromlistViewShowFolder[2] + " (" + GetPathFromlistViewShowFolder[3] + ")", counterColumnListPathSize, HorizontalAlignment.Left);
  183.  
  184. foreach (string b in Directory.GetDirectories(listViewShowFolder.Items[i].Text, "*", SearchOption.TopDirectoryOnly))
  185. {
  186. storeNamePath.Add(b);
  187. if (storeLastNamePath.Contains(b.Split('\\').Last().ToString()))
  188. {
  189. storeTotalSize = 0;
  190.  
  191.  
  192. }
  193. else
  194. {
  195. storeLastNamePath.Add(b.Split('\\').Last().ToString());
  196.  
  197. }
  198. }
  199.  
  200. }
  201. }
  202. listViewShowPathSize.Columns.Add("Total", 100, HorizontalAlignment.Left);
  203.  
  204.  
  205. }
  206.  
  207.  
  208. public void GetSizeToTheList()
  209. {
  210. foreach (string AllLastFolderName in storeLastNamePath)
  211. {
  212.  
  213. storeTotalSize = 0;
  214. int countArray = 2;
  215.  
  216. foreach (string StorePath in storeNamePath)
  217. {
  218. TheLastFolderName = StorePath.Split('\\').Last();
  219.  
  220. countAllArrayList++;
  221.  
  222. if (AllLastFolderName.Equals(TheLastFolderName))
  223. {
  224.  
  225. arr[1] = TheLastFolderName;
  226. CounterPositionofList = 0;
  227.  
  228. CalculateTheSameNameFolder();
  229. getSize(StorePath);
  230.  
  231.  
  232.  
  233.  
  234.  
  235. for (int i = 0; i < listViewShowFolder.Items.Count; i++)
  236. {
  237. if (listViewShowFolder.Items[i].Checked == true)
  238. {
  239. GetPathFromlistViewShowFolder = listViewShowFolder.Items[i].Text.Split('\\');
  240. GetFullPath = StorePath.Split('\\');
  241. if (GetPathFromlistViewShowFolder[2] + GetPathFromlistViewShowFolder[3] == GetFullPath[2] + GetFullPath[3])
  242. {
  243.  
  244. StoreSizeofFolder[CounterPositionofList] = TotalSizeOfFolder;
  245. storeCounterPositionToArray[CounterPositionofList] = CounterPositionofList + 2;
  246. CounterPositionofList++;
  247.  
  248.  
  249. }
  250. else
  251. {
  252.  
  253. arr[CounterPositionofList + 2] = "0";
  254.  
  255. CounterPositionofList++;
  256. }
  257.  
  258. }
  259.  
  260. }
  261. countArray++;
  262.  
  263. }
  264.  
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. storeLastTotalSizeToList();
  272.  
  273.  
  274. // CalculateTheSameNameFolder();
  275.  
  276. ClearStoreSizeofFolder();
  277. CounterPositionofList = 0;
  278.  
  279. }
  280.  
  281.  
  282. }
  283. public void storeLastTotalSizeToList()
  284. {
  285. for (int i = 0; i < storeCounterPositionToArray.Length; i++)
  286. {
  287.  
  288. arr[storeCounterPositionToArray[i]] = StoreSizeofFolder[i].ToString();
  289.  
  290. }
  291.  
  292. }
  293.  
  294. public void CalculateTheSameNameFolder()
  295. {
  296. //if (countAllArrayList >= storeNamePath.Count)
  297. //{
  298. // CounterPositionofList = 0;
  299. // for (int i = 0; i < listViewShowFolder.Items.Count; i++)
  300. // {
  301. // if (listViewShowFolder.Items[i].Checked == true)
  302. // {
  303. // try
  304. // {
  305.  
  306. // storeTotalSize = storeTotalSize + double.Parse(arr[CounterPositionofList + 2]);
  307. // CounterPositionofList++;
  308.  
  309.  
  310. // }
  311. // catch (Exception eee)
  312. // {
  313.  
  314. // }
  315. // }
  316.  
  317. // }
  318.  
  319. // arr[CounterPositionofList + 2] = storeTotalSize.ToString();
  320.  
  321.  
  322. arr[0] = counterNoListViewFolder.ToString();
  323. ListViewItem lvi = new ListViewItem(arr);
  324. listViewShowPathSize.Items.Add(lvi);
  325.  
  326. listViewShowPathSize.Refresh();
  327. listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
  328. counterNoListViewFolder++;
  329. // }
  330.  
  331. }
  332. public void ClearStoreSizeofFolder()
  333. {
  334. for (int i = 0; i < storeCounterPositionToArray.Length; i++)
  335. {
  336. StoreSizeofFolder[i] = 0;
  337. }
  338. }
  339.  
  340. public void applyToTask()
  341. {
  342.  
  343. foreach(string fullpath in storeNamePath){
  344. arr[0] = counterNoListViewFolder.ToString();
  345. arr[1] = fullpath.Split('\\').Last();
  346. arr[2] = fullpath;
  347. ListViewItem lvi = new ListViewItem(arr);
  348. listViewShowPathSize.Items.Add(lvi);
  349.  
  350. listViewShowPathSize.Refresh();
  351. listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
  352. counterNoListViewFolder++;
  353.  
  354.  
  355. getSize(fullpath);
  356.  
  357. Total = 0;
  358. totalSize = 0;
  359.  
  360. };
  361.  
  362. }
  363.  
  364.  
  365. private void btnStart_Click(object sender, EventArgs e)
  366. {
  367.  
  368.  
  369.  
  370. applyToTask();
  371.  
  372. // foreach(string fullpath in storeNamePath){
  373. // arr[0] = counterNoListViewFolder.ToString();
  374. // arr[1] = fullpath.Split('\\').Last();
  375. // arr[2] = fullpath;
  376. // ListViewItem lvi = new ListViewItem(arr);
  377. // listViewShowPathSize.Items.Add(lvi);
  378.  
  379. // listViewShowPathSize.Refresh();
  380. // listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
  381. // counterNoListViewFolder++;
  382.  
  383.  
  384. // getSize(fullpath);
  385.  
  386. // Total = 0;
  387. // totalSize = 0;
  388.  
  389. //};
  390.  
  391. //CheckBoxListFolder(); //check box for check size when checkbox is true check size else skip
  392.  
  393. //GetSizeToTheList(); //check what folder that the same name and check what folder not the same name to get size to list
  394. ////for (int i = 1; i <= 1000; i++)
  395. ////{
  396. //// ListViewItem LVI = new ListViewItem("Track " + i);
  397. //// LVI.SubItems.Add("Updated");
  398.  
  399. //// listViewShowPathSize.Items.Add(LVI);
  400. //// listViewShowPathSize.TopItem = LVI;
  401. //// listViewShowPathSize.EnsureVisible(listViewShowPathSize.Items.Count - 1);
  402. // // Application.DoEvents();
  403. // }
  404. MessageBox.Show("Already!");
  405. }
  406.  
  407. private void btnGetFolder_Click(object sender, EventArgs e)
  408. {
  409.  
  410.  
  411. readLocationFromFile(txtPathFile.Text);
  412.  
  413. foreach (string getLocation in StoreLocation)
  414. {
  415.  
  416. listViewShowFolder.CheckBoxes = true;
  417. ListViewItem lvi = new ListViewItem(getLocation);
  418. listViewShowFolder.Items.Add(lvi);
  419. listViewShowFolder.Items[countListviewShowFolder].Checked = true;
  420.  
  421. countListviewShowFolder++;
  422. }
  423.  
  424. btnGetFolder.Enabled = false;
  425. }
  426.  
  427. private void btnClear_Click(object sender, EventArgs e)
  428. {
  429. StoreLocation.Clear();
  430. listViewShowFolder.Items.Clear();
  431. listViewShowPathSize.Items.Clear();
  432. countListviewShowFolder = 0;
  433. counterNoListViewFolder = 1;
  434. txtPathFile.Clear();
  435. GetAllLocation.Clear();
  436. btnStart.Enabled = true;
  437. btnGetFolder.Enabled = true;
  438. }
  439.  
  440. private void btnCopy_Click(object sender, EventArgs e)
  441. {
  442. string clipText=null;
  443. for (int i = 0; i < listViewShowPathSize.Columns.Count; i++)
  444. {
  445. clipText += listViewShowPathSize.Columns[i].Text + "\t";
  446.  
  447. }
  448.  
  449. clipText += Environment.NewLine;
  450. foreach(ListViewItem lv in listViewShowPathSize.SelectedItems){
  451. for (int i = 0; i < listViewShowPathSize.Columns.Count;i++ )
  452. {
  453. clipText+=lv.SubItems[i].Text + "\t";
  454.  
  455. }
  456. clipText += Environment.NewLine;
  457.  
  458. }
  459.  
  460.  
  461. Clipboard.SetText(clipText);
  462.  
  463.  
  464.  
  465.  
  466.  
  467. }
  468.  
  469. private void tabPageCheck_Click(object sender, EventArgs e)
  470. {
  471.  
  472. }
  473.  
  474. private void CheckSize_Load(object sender, EventArgs e)
  475. {
  476.  
  477. }
  478.  
  479. private void listViewShowPathSize_SelectedIndexChanged(object sender, EventArgs e)
  480. {
  481. if (listViewShowPathSize.SelectedItems.Count >= 0)
  482. {
  483.  
  484. btnCopy.Enabled = true;
  485.  
  486. }
  487. else {
  488. btnCopy.Enabled = false;
  489. }
  490. }
  491.  
  492.  
  493. }
  494.  
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement