C # _ IO operator _ queries the space occupied by each subfolders in a specified folder,

Source: Internet
Author: User

C # _ IO operator _ queries the space occupied by each subfolders in a specified folder,
1. Preface

Disk memory is used up too much. If you want to check the memory occupied by those folders, you can find useless files to delete them.

2. Code
Static void Main (string [] args) {while (true) {// specify the space occupied by each subfolders in the folder. writeLine ("-------------------- query the space occupied by all subfolders in the specified path -----------------"); Console. writeLine ("Enter the Path to query:"); string Path = Console. readLine (); Dictionary <string, long> dic = new Dictionary <string, long> (); // whether the path exists if (! Directory. Exists (Path) {Console. WriteLine ("the input Path does not exist. Please enter the correct Path! "); Console. writeLine ("");} else {// define a DirectoryInfo object DirectoryInfo di = new DirectoryInfo (Path); // use the GetFiles method, obtain all the files in the di directory foreach (FileInfo fi in di. getFiles () {// Add the information to the dictionary dic. add (fi. fullName, fi. length);} // get all the folders in di and save them to a new array of objects. DirectoryInfo [] dis = di. getDirectories (); if (dis. length> 0) for (int I = 0; I <dis. length; I ++) {long length = GetDirectoryLength (dis [I]. fullName); dic. Add (dis [I]. fullName, length);} // write the statement in writing, sort the Dictionary by value var dicSort = from objDic in dic orderby objDic. value descending select objDic; foreach (KeyValuePair <string, long> dic1 in dicSort) {// change KB to M and/1024 to K, and then/1024 to M Console. writeLine (string. format ("{0 }:{ 1} {2}", dic1.Key. toString (), Math. round (Convert. toDouble (dic1.Value)/1024/1024, 2), "M") ;}}// specify the space occupied by the folder public static long GetDir EctoryLength (string dirPath) {// determines whether the specified path exists. if not, exit if (! Directory. exists (dirPath) return 0; long len = 0; // define a DirectoryInfo object DirectoryInfo di = new DirectoryInfo (dirPath); // use the GetFiles method, obtain the size of all objects in the di directory. foreach (FileInfo fi in di. getFiles () {len + = fi. length;} // obtain all the folders in di and store them in a new object array for Recursive DirectoryInfo [] dis = di. getDirectories (); if (dis. length> 0) {for (int I = 0; I <dis. length; I ++) {len + = GetDirectoryLength (dis [I]. fullName) ;}} return len ;}

Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.