C# calculate disk size

来源:https://www.cnblogs.com/Fred1987/archive/2019/11/21/11908051.html
-Advertisement-
Play Games

static void Main(string[] args) { string dir = @"C:\"; string[] dirs=Directory.GetDirectories(dir); long totalSize = 0; if(dirs!=null && di... ...


    static void Main(string[] args)
        {
            string dir = @"C:\";
            string[] dirs=Directory.GetDirectories(dir);
            long totalSize = 0;
            if(dirs!=null && dirs.Any())
            {
                foreach(string dr in dirs)
                {
                    var size = new DirectoryInfo(dr).GetDirectorySize();
                    totalSize += size;
                    Console.WriteLine($"dir:{dr},size:{size}");
                }
            }
            Console.WriteLine($"totalSize:{totalSize}");
            System.Diagnostics.Debug.WriteLine($"totalSize:{totalSize}");
        }  

static class DirHelper
    {
        public static long GetDirectorySize(this System.IO.DirectoryInfo directoryInfo, bool recursive = true)
        {
            var startDirectorySize = default(long);
            try
            {
                
                if (directoryInfo == null || !directoryInfo.Exists)
                    return startDirectorySize; //Return 0 while Directory does not exist.

                //Add size of files in the Current Directory to main size.
                foreach (var fileInfo in directoryInfo.GetFiles())
                    System.Threading.Interlocked.Add(ref startDirectorySize, fileInfo.Length);

                if (recursive) //Loop on Sub Direcotries in the Current Directory and Calculate it's files size.
                    System.Threading.Tasks.Parallel.ForEach(directoryInfo.GetDirectories(), (subDirectory) =>
                System.Threading.Interlocked.Add(ref startDirectorySize, GetDirectorySize(subDirectory, recursive)));

                  //Return full Size of this Directory.
            }
            catch
            {

            }
            return startDirectorySize;
        }
    }

 

static void DiskDemo()
        {
            string dir = @"C:\Windows\";
            string[] dirs = Directory.GetDirectories(dir);
            long totalSize = 0;
            StringBuilder builder = new StringBuilder();
            List<Dir> dirList = new List<Dir>();
            if (dirs != null && dirs.Any())
            {
                foreach (string dr in dirs)
                {
                    var size = new DirectoryInfo(dr).GetDirectorySize();
                    Dir d = new Dir();
                    d.DirName = dr;
                    d.DirSize = size;
                    dirList.Add(d);
                    totalSize += size;
                }
            }

            foreach (var dd in dirList.OrderByDescending(x => x.DirSize))
            {
                System.Diagnostics.Debug.WriteLine(dd);
                Console.WriteLine(dd);
            }
            System.Diagnostics.Debug.WriteLine($"totalSize:{totalSize}");
        }



 class Dir
    {
        public string DirName { get; set; }
        public long DirSize { get; set; }

        public override string ToString()
        {
            return $"DirName:{DirName},DirSize:{DirSize}";
        }
    }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 環境:vmware centos7.4 2cpu 2核心 工具:ab壓力測試工具 測試對象:sso單點登錄系統 電腦:win10 4核 項目環境:flask+uwsgi+nginx(uwsgi 2進程,4線程) 1. 100個用戶,總共100個請求 測試截圖 2. 500個用戶,總共500個請求 測 ...
  • 假設需求開發一個叫做 helloWord 的擴展。擴展里有一個函數,helloWord()。 echo helloWord('Tom'); //返回:Hello World: Tom 本地環境PHP版本:5.6.9系統:Linux CentOS release 6.5 (Final) 最終效果 實現 ...
  • 1.內容介紹 深入解析tp5.1與laravel 中Facade底層原理實現 1. 什麼是Facade 2. 為什麼需要有什麼好處 3. Facade實現原理 4. 功能實現、 5. 容器註入 2.知識講解 0.什麼是Facade??為什麼需要?有什麼好處 專業解釋: 門面模式(Facade)又稱外 ...
  • Hangman 游戲簡介 百度百科 列印Hangman 其它 開始游戲 給定一個需要猜測的單詞開始游戲 def start_game(word): 已經猜錯的詞 wrong = '' 將未猜出的以 顯示 secret = hide(word) 記錄還剩多少個 ,如果為0,則為全部猜中 secret_ ...
  • 執行下麵這倆命令,找到對應的IP,增加host就能解決 nslookup github.global.ssl.fastly.Netnslookup github.com root@tao-PC:/var/www/html/go-project/test# nslookup github.global ...
  • 在前面簡單描述了下服務層,SOA面向服務架構,架構設計-業務邏輯層,以及一些面向設計原則理解和軟體架構設計箴言。這篇博客我們將繼續進入我們的下一層:數據訪問層。無論你用的是什麼開發模式或者是業務模式,到最後最必須具有持久化機制,持久化到持久化介質,並能對數據進行讀取和寫入CRUD。這就是數據訪問層。 ...
  • .NET Core 獲取資料庫上下文實例的方法和配置連接字元串 [TOC] 假設資料庫就兩個表:User、Blogs, 模型類如下 資料庫上下文大致這樣 ASP.NET Core 註入 ASP.NET Core 的資料庫註入是最為簡單方便的了,在 ConfigureServices 配置即可。 然後 ...
  • 一. 委托的分類 通過用什麼類型的方法來聲明為委托,可以分為兩類: 1. 委托靜態方法:把一個靜態方法給委托 2. 委托實例方法:把一個實例對象的成員方法給委托 (這兩個名字是博主取的,可能不是很專業只是為了好區分) 二. 原理部分 委托是將函數指針和實例對象打包在一起的類,它有兩個重要的成員,一個 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...