Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Collections;
- using System.Threading;
- using System.Diagnostics;
- namespace CheckSize
- {
- public partial class CheckSize : Form
- {
- int itemToList = 0;
- double totalSize;
- static double TotalSizeOfFolderByte;
- static double TotalSizeOfFolder;
- static double sizeOfDirKb;
- static string[] arr = new string[30];
- int countListviewShowFolder = 0;
- int counterNoListViewFolder = 1;
- int counterColumnListPathSize = 80;
- string[] GetPathFromlistViewShowFolder;
- string[] GetFullPath;
- double[] StoreSizeofFolder =new double[30];
- int CounterPositionofList=0;
- int[] storeCounterPositionToArray=new int[30];
- ArrayList eeee = new ArrayList();
- string[] LocationGet;
- double storeTotalSize = 0;
- ArrayList storeNamePath = new ArrayList();
- ArrayList storeLastNamePath = new ArrayList();
- string TheLastFolderName=null;
- int countAllArrayList = 0;
- ArrayList StoreLocation = new ArrayList();
- ArrayList storeLocationcheckSize = new ArrayList();
- string[] storeAllLocation = new string[1000];
- ArrayList GetAllLocation = new ArrayList();
- public CheckSize()
- {
- InitializeComponent();
- }
- public void getSize(string pathLocation)
- {
- try
- {
- DirectoryInfo dInfo = new DirectoryInfo(@pathLocation);
- sizeOfDirKb = DirectorySize(dInfo, true) / (1024);
- TotalSizeOfFolderByte = sizeOfDirKb / (1024 * 1024);
- if (TotalSizeOfFolderByte < 100)
- {
- TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte * 100) / 100;
- }
- else if (TotalSizeOfFolderByte >= 100)
- {
- TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte);
- }
- itemToList++;
- }
- catch (Exception e)
- {
- }
- // itemToList++;
- }
- double Total=0;
- double total1 = 0;
- double DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
- {
- Application.DoEvents();
- totalSize = dInfo.EnumerateFiles()
- .Sum(file => file.Length);
- Total = Total + totalSize / 1024 / 1024 / 1024;
- total1 = Total;
- if (total1 < 100)
- {
- total1 = Math.Floor(total1 * 100) / 100;
- }
- else if (total1 >= 100)
- {
- total1 = Math.Floor(total1);
- }
- if (includeSubDir)
- {
- totalSize += dInfo.EnumerateDirectories()
- .Sum(dir => DirectorySize(dir, true));
- }
- // Total = totalSize / (1024 * 1024);
- //Console.WriteLine(Total);
- listViewShowPathSize.Items[itemToList].SubItems[4].Text = (total1).ToString();
- listViewShowPathSize.EnsureVisible(listViewShowPathSize.Items.Count - 1);
- return totalSize;
- }
- private void btnChooseFile_Click(object sender, EventArgs e)
- {
- listViewShowFolder.Items.Clear();
- listViewShowPathSize.Items.Clear();
- btnGetFolder.Enabled = true;
- btnStart.Enabled = true;
- countListviewShowFolder = 0;
- counterNoListViewFolder = 1;
- OpenFileDialog ChooseFile = new OpenFileDialog();
- DialogResult ResultPathFile = ChooseFile.ShowDialog();
- if (ResultPathFile == DialogResult.OK)
- {
- txtPathFile.Clear();
- txtPathFile.Text = ChooseFile.FileName;
- }
- }
- public void readLocationFromFile(string Location)
- {
- LocationGet = File.ReadAllLines(Location);
- foreach (string a in LocationGet)
- {
- StoreLocation.Add(a);
- foreach (string b in Directory.GetDirectories(a, "*", SearchOption.TopDirectoryOnly))
- {
- GetAllLocation.Add(b);
- }
- }
- }
- public void CheckBoxListFolder()
- {
- for (int i = 0; i < listViewShowFolder.Items.Count; i++)
- {
- if (listViewShowFolder.Items[i].Checked == true)
- {
- GetPathFromlistViewShowFolder = listViewShowFolder.Items[i].Text.Split('\\');
- listViewShowPathSize.Columns.Add(GetPathFromlistViewShowFolder[2] + " (" + GetPathFromlistViewShowFolder[3] + ")", counterColumnListPathSize, HorizontalAlignment.Left);
- foreach (string b in Directory.GetDirectories(listViewShowFolder.Items[i].Text, "*", SearchOption.TopDirectoryOnly))
- {
- storeNamePath.Add(b);
- if (storeLastNamePath.Contains(b.Split('\\').Last().ToString()))
- {
- storeTotalSize = 0;
- }
- else
- {
- storeLastNamePath.Add(b.Split('\\').Last().ToString());
- }
- }
- }
- }
- listViewShowPathSize.Columns.Add("Total", 100, HorizontalAlignment.Left);
- }
- public void GetSizeToTheList()
- {
- foreach (string AllLastFolderName in storeLastNamePath)
- {
- storeTotalSize = 0;
- int countArray = 2;
- foreach (string StorePath in storeNamePath)
- {
- TheLastFolderName = StorePath.Split('\\').Last();
- countAllArrayList++;
- if (AllLastFolderName.Equals(TheLastFolderName))
- {
- arr[1] = TheLastFolderName;
- CounterPositionofList = 0;
- CalculateTheSameNameFolder();
- getSize(StorePath);
- for (int i = 0; i < listViewShowFolder.Items.Count; i++)
- {
- if (listViewShowFolder.Items[i].Checked == true)
- {
- GetPathFromlistViewShowFolder = listViewShowFolder.Items[i].Text.Split('\\');
- GetFullPath = StorePath.Split('\\');
- if (GetPathFromlistViewShowFolder[2] + GetPathFromlistViewShowFolder[3] == GetFullPath[2] + GetFullPath[3])
- {
- StoreSizeofFolder[CounterPositionofList] = TotalSizeOfFolder;
- storeCounterPositionToArray[CounterPositionofList] = CounterPositionofList + 2;
- CounterPositionofList++;
- }
- else
- {
- arr[CounterPositionofList + 2] = "0";
- CounterPositionofList++;
- }
- }
- }
- countArray++;
- }
- }
- storeLastTotalSizeToList();
- // CalculateTheSameNameFolder();
- ClearStoreSizeofFolder();
- CounterPositionofList = 0;
- }
- }
- public void storeLastTotalSizeToList()
- {
- for (int i = 0; i < storeCounterPositionToArray.Length; i++)
- {
- arr[storeCounterPositionToArray[i]] = StoreSizeofFolder[i].ToString();
- }
- }
- public void CalculateTheSameNameFolder()
- {
- //if (countAllArrayList >= storeNamePath.Count)
- //{
- // CounterPositionofList = 0;
- // for (int i = 0; i < listViewShowFolder.Items.Count; i++)
- // {
- // if (listViewShowFolder.Items[i].Checked == true)
- // {
- // try
- // {
- // storeTotalSize = storeTotalSize + double.Parse(arr[CounterPositionofList + 2]);
- // CounterPositionofList++;
- // }
- // catch (Exception eee)
- // {
- // }
- // }
- // }
- // arr[CounterPositionofList + 2] = storeTotalSize.ToString();
- arr[0] = counterNoListViewFolder.ToString();
- ListViewItem lvi = new ListViewItem(arr);
- listViewShowPathSize.Items.Add(lvi);
- listViewShowPathSize.Refresh();
- listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
- counterNoListViewFolder++;
- // }
- }
- public void ClearStoreSizeofFolder()
- {
- for (int i = 0; i < storeCounterPositionToArray.Length; i++)
- {
- StoreSizeofFolder[i] = 0;
- }
- }
- public void applyToTask()
- {
- foreach(string fullpath in storeNamePath){
- arr[0] = counterNoListViewFolder.ToString();
- arr[1] = fullpath.Split('\\').Last();
- arr[2] = fullpath;
- ListViewItem lvi = new ListViewItem(arr);
- listViewShowPathSize.Items.Add(lvi);
- listViewShowPathSize.Refresh();
- listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
- counterNoListViewFolder++;
- getSize(fullpath);
- Total = 0;
- totalSize = 0;
- };
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- applyToTask();
- // foreach(string fullpath in storeNamePath){
- // arr[0] = counterNoListViewFolder.ToString();
- // arr[1] = fullpath.Split('\\').Last();
- // arr[2] = fullpath;
- // ListViewItem lvi = new ListViewItem(arr);
- // listViewShowPathSize.Items.Add(lvi);
- // listViewShowPathSize.Refresh();
- // listViewShowPathSize.Items[listViewShowPathSize.Items.Count - 1].EnsureVisible();
- // counterNoListViewFolder++;
- // getSize(fullpath);
- // Total = 0;
- // totalSize = 0;
- //};
- //CheckBoxListFolder(); //check box for check size when checkbox is true check size else skip
- //GetSizeToTheList(); //check what folder that the same name and check what folder not the same name to get size to list
- ////for (int i = 1; i <= 1000; i++)
- ////{
- //// ListViewItem LVI = new ListViewItem("Track " + i);
- //// LVI.SubItems.Add("Updated");
- //// listViewShowPathSize.Items.Add(LVI);
- //// listViewShowPathSize.TopItem = LVI;
- //// listViewShowPathSize.EnsureVisible(listViewShowPathSize.Items.Count - 1);
- // // Application.DoEvents();
- // }
- MessageBox.Show("Already!");
- }
- private void btnGetFolder_Click(object sender, EventArgs e)
- {
- readLocationFromFile(txtPathFile.Text);
- foreach (string getLocation in StoreLocation)
- {
- listViewShowFolder.CheckBoxes = true;
- ListViewItem lvi = new ListViewItem(getLocation);
- listViewShowFolder.Items.Add(lvi);
- listViewShowFolder.Items[countListviewShowFolder].Checked = true;
- countListviewShowFolder++;
- }
- btnGetFolder.Enabled = false;
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- StoreLocation.Clear();
- listViewShowFolder.Items.Clear();
- listViewShowPathSize.Items.Clear();
- countListviewShowFolder = 0;
- counterNoListViewFolder = 1;
- txtPathFile.Clear();
- GetAllLocation.Clear();
- btnStart.Enabled = true;
- btnGetFolder.Enabled = true;
- }
- private void btnCopy_Click(object sender, EventArgs e)
- {
- string clipText=null;
- for (int i = 0; i < listViewShowPathSize.Columns.Count; i++)
- {
- clipText += listViewShowPathSize.Columns[i].Text + "\t";
- }
- clipText += Environment.NewLine;
- foreach(ListViewItem lv in listViewShowPathSize.SelectedItems){
- for (int i = 0; i < listViewShowPathSize.Columns.Count;i++ )
- {
- clipText+=lv.SubItems[i].Text + "\t";
- }
- clipText += Environment.NewLine;
- }
- Clipboard.SetText(clipText);
- }
- private void tabPageCheck_Click(object sender, EventArgs e)
- {
- }
- private void CheckSize_Load(object sender, EventArgs e)
- {
- }
- private void listViewShowPathSize_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listViewShowPathSize.SelectedItems.Count >= 0)
- {
- btnCopy.Enabled = true;
- }
- else {
- btnCopy.Enabled = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement