Skip to content

techtrendings.com

Let's explore

Menu
Menu

Calculate size of drive in C# – How to find directory drive?

Posted on November 11, 2022November 11, 2022 by Avidlearner

In this post, we will be discussing how to find the directory drive and the size of drive in C#. This can be useful where application might want to check for drive size or remaining size of drive before getting started. Below is the snippet of implementation:

FileInfo f = new FileInfo(dirPath); //dirPath is the directory path
string drive = Path.GetPathRoot(f.FullName); //Get the drive name - if path is C:\user\desktop it will return "C:\"

To get the drive information in C#, one can use DriveInfo Class from System.IO namespace. A single DriveInfo object models one computer drive. Through that object we can access the drive’s information, like its name label,drive’s capacity, like free space amount or total size.
Below are the code snippets for getting the disk space of a drive –

DriveInfo dInfo = new DriveInfo(drive); //Create instance of DriveInfo Class
long availFreeSize = dInfo.AvailableFreeSpace; //this will get available freespace
long totalFreeSize = dInfo.TotalFreeSpace; //returns drive's total free space.
long totalSize = dInfo.TotalSize; //returns drive's total capacity.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme