TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# DriveInfo Examples

Use the DriveInfo class, calling DriveInfo.GetDrives and AvailableFreeSpace.
DriveInfo. Computers often have several drives. On Windows, these use letters such as C or D as names. In the .NET Framework, the DriveInfo class provides helper methods for checking these drives. We can get all drives.
Example. Often you may already know the drive name you want to access. Most Windows computers use the C drive name. In this program, we use the DriveInfo constructor and pass the one-character string literal "C".ConstructorString Literal

Then: We display the values returned by the properties on the DriveInfo instance. The free space properties return long values.

Long

Output: The free space numbers are in bytes. It is possible to convert these to megabytes and gigabytes using custom helper methods.

Convert Bytes, Megabytes
C# program that uses DriveInfo using System; using System.IO; class Program { static void Main() { DriveInfo info = new DriveInfo("C"); // [1] Print sizes. Console.WriteLine(info.AvailableFreeSpace); Console.WriteLine(info.TotalFreeSpace); Console.WriteLine(info.TotalSize); Console.WriteLine(); // [2] Format and type. Console.WriteLine(info.DriveFormat); Console.WriteLine(info.DriveType); Console.WriteLine(); // [3] Name and directories. Console.WriteLine(info.Name); Console.WriteLine(info.RootDirectory); Console.WriteLine(info.VolumeLabel); Console.WriteLine(); // [4] Ready. Console.WriteLine(info.IsReady); } } Output 682166767616 682166767616 984045580288 NTFS Fixed C:\ C:\ OS True
Example 2. Sometimes a program will need to get an array of all the drives on the computer. The DriveInfo.GetDrives method is available for this purpose. It returns an array of DriveInfo class instances.

Here: We use the foreach-loop on the result of the GetDrives method. The code from the first example could be added to the loop.

Foreach
C# program that gets all drives using System; using System.IO; class Program { static void Main() { // Print all drive names. var drives = DriveInfo.GetDrives(); foreach (DriveInfo info in drives) { Console.WriteLine(info.Name); } } } Output C:\ D:\
Summary. Helper methods such as DriveInfo are not usually needed in programs. But when they are needed, they make programs much easier to develop. The DriveInfo class can be combined with the DirectoryInfo and FileInfo classes.FileInfo
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf