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: