الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

code to read the folder size

مغلق
بدأه KARIMSOFT في 24 سبتمبر 2006 · 2 رد · 627 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هدية رمضان لاحبائي في الله

كل سنه وانتم طيبين

كود

 using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Resources;

namespace sizeDir
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Data.DataSet dataSet1;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Data.DataTable dataTable1;
		private System.Data.DataColumn dataColumn1;
		private System.Data.DataColumn dataColumn2;
		private System.Data.DataColumn dataColumn3;
		private System.Data.DataColumn dataColumn4;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button2;
		private System.Data.DataColumn dataColumn5;
		private System.Windows.Forms.TextBox textBox3;
		private System.ComponentModel.Container components = null;
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.dataSet1 = new System.Data.DataSet();
			this.dataTable1 = new System.Data.DataTable();
			this.dataColumn1 = new System.Data.DataColumn();
			this.dataColumn2 = new System.Data.DataColumn();
			this.dataColumn3 = new System.Data.DataColumn();
			this.dataColumn4 = new System.Data.DataColumn();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.dataColumn5 = new System.Data.DataColumn();
			this.textBox3 = new System.Windows.Forms.TextBox();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(32, 16);
			this.label1.TabIndex = 16;
			this.label1.Text = "path";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(520, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 24);
			this.button1.TabIndex = 17;
			this.button1.Text = "run";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(56, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(448, 20);
			this.textBox1.TabIndex = 15;
			this.textBox1.Text = "z:\\";
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Size Group";
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "T1";
			this.dataGrid1.DataSource = this.dataSet1;
			this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 82);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(760, 448);
			this.dataGrid1.TabIndex = 21;
			this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
			this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
			// 
			// dataSet1
			// 
			this.dataSet1.DataSetName = "NewDataSet";
			this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
			this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
																		  this.dataTable1});
			// 
			// dataTable1
			// 
			this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
																			  this.dataColumn1,
																			  this.dataColumn2,
																			  this.dataColumn3,
																			  this.dataColumn4,
																			  this.dataColumn5});
			this.dataTable1.TableName = "T1";
			// 
			// dataColumn1
			// 
			this.dataColumn1.Caption = "Folder";
			this.dataColumn1.ColumnName = "col0";
			// 
			// dataColumn2
			// 
			this.dataColumn2.Caption = "Size";
			this.dataColumn2.ColumnName = "col1";
			this.dataColumn2.DataType = typeof(long);
			// 
			// dataColumn3
			// 
			this.dataColumn3.Caption = "Size";
			this.dataColumn3.ColumnName = "col2";
			// 
			// dataColumn4
			// 
			this.dataColumn4.Caption = "Ghraph";
			this.dataColumn4.ColumnName = "col3";
			// 
			// progressBar1
			// 
			this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.progressBar1.Location = new System.Drawing.Point(0, 59);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(760, 23);
			this.progressBar1.TabIndex = 22;
			// 
			// textBox2
			// 
			this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet1, "T1.col0"));
			this.textBox2.Location = new System.Drawing.Point(112, 32);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(0, 20);
			this.textBox2.TabIndex = 23;
			this.textBox2.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(0, 32);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 24);
			this.button2.TabIndex = 24;
			this.button2.Text = "Back";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// dataColumn5
			// 
			this.dataColumn5.ColumnName = "col4";
			this.dataColumn5.DataType = typeof(bool);
			// 
			// textBox3
			// 
			this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet1, "T1.col4"));
			this.textBox3.Location = new System.Drawing.Point(176, 32);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(0, 20);
			this.textBox3.TabIndex = 25;
			this.textBox3.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(760, 530);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		private void button1_Click(object sender, System.EventArgs e)
		{ 
			long lDirSize=0;
			long lTotalSize=0;
			long lFileSize=0;

			DirectoryInfo rDir = new DirectoryInfo(textBox1.Text);
			progressBar1.Maximum=rDir.GetDirectories().Length;
			progressBar1.Value=0;

			dataSet1.Clear();

			FileInfo[] rFile = rDir.GetFiles();
			foreach (FileInfo rrFile in rFile) 
			{
				lFileSize+=rrFile.Length;
				dataSet1.Tables[0].Rows.Add(new Object[] {rrFile.Name ,rrFile.Length.ToString(),GetGroupSize(rrFile.Length).ToString(),1,1});
			}
			foreach (string temp in Directory.GetDirectories(textBox1.Text))
			{
				string strDirName=temp.Substring(Path.GetDirectoryName(temp).Length).ToString();

				DirectoryInfo DI =new DirectoryInfo(temp);

				//				ListViewItem L;
				//				L = listView1.Items.Add(strDirName);
				lDirSize=DirSize(DI);
				//				L.SubItems.Add(lDirSize.ToString());
				//				L.SubItems.Add(GetGroupSize(lDirSize));

				//-------------------------
				dataSet1.Tables[0].Rows.Add(new Object[] {strDirName,lDirSize.ToString(),GetGroupSize(lDirSize).ToString()});
				//-------------------------

				lTotalSize+=lDirSize;
				lDirSize=0;
				progressBar1.Value+=1;
			}
			this.Text=(lFileSize+lTotalSize).ToString()+"---"+GetGroupSize(lFileSize+lTotalSize).ToString();
		}
		public static long DirSize(DirectoryInfo d) 
		{ 
			long lSize = 0; 

			FileInfo[] fis = d.GetFiles();
			foreach (FileInfo fi in fis) lSize += fi.Length; 

			DirectoryInfo[] dis = d.GetDirectories();
			foreach (DirectoryInfo di in dis) lSize += DirSize(di); 

			return lSize; 
		}
		public static long FolderFileSize(string path)
		{
			long size = 0;
			try
			{
				FileInfo [] files = (new DirectoryInfo(path)).GetFiles();
				foreach(FileInfo file in files)
				{
					size += file.Length;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return size;
		}

		public static long FolderSize(string path)
		{
			long Fsize = 0;
			try
			{
				Fsize = FolderFileSize(path);
				DirectoryInfo [] folders = (new DirectoryInfo(path)).GetDirectories();
				foreach(DirectoryInfo folder in folders)
				{
					Fsize += FolderSize(folder.FullName);
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return Fsize;
		}
		public static string GetGroupSize(long lSize)
		{
			string sGroupSize="";
			long lOut;
			if (lSize<=1024)
			{
				sGroupSize =lSize +" Bytes";
				return sGroupSize;
			}

			if (lSize<=1048576)
			{
				lOut=lSize/1024;
				sGroupSize =lOut.ToString()+","+ (lSize-lOut).ToString() +" KB";
				return sGroupSize;
			}

			if (lSize<1073741824)
			{
				lOut=lSize/1048576;
				sGroupSize =lOut.ToString()+","+ ((lSize-lOut)/1024).ToString() +" MB";
				return sGroupSize;
			}

			if (lSize<=1099511627776)
			{
				lOut=lSize/1073741824;
				sGroupSize =lOut.ToString()+","+ ((lSize-lOut)/1048576).ToString() +" GB";
				return sGroupSize;
			}

			if (lSize<=1125899906842624)
			{
				lOut=lSize/1099511627776;
				sGroupSize =lOut.ToString()+","+ ((lSize-lOut)/1073741824).ToString() +" TB";
				return sGroupSize;
			}
			return sGroupSize;
		}	
		string tt="";
		private void dataGrid1_Click(object sender, System.EventArgs e)
		{
			if(tt=="")tt=textBox1.Text;
			if(textBox3.Text=="")
			{
				textBox1.Text=tt+textBox2.Text;
			}
			else
			{
				//system.Resources.S
			}
		}

		private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
		{
			button1_Click(sender,e);
			tt="";
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			string[] a=textBox1.Text.Split('\\');
			textBox2.Text=a[a.Length-1];
			textBox1.Text=textBox1.Text.Substring(0,textBox2.Text.Length-a.Length+1);
			button1_Click(sender,e);
			tt="";
		}
	}
}
#2

مشكور على هذا المجهود المقدر.

#3

جزاك الله خيرا

سأل الممكن المستحيل اين تسكن؟ قال في عقول الضعفاء

اشهد أن لا اله الا الله و اشهد أن محمداًً رسول الله

هذا الموضوع مغلق.

مواضيع مشابهة