Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
921 views
in .Net Framework by 39 42 54
I am currently developing a C# solution and I need to gather additional information about a custom drive disk, including details like TotalSize, AvailableFreeSpace, and BusySpace. How can I achieve this within my solution?

1 Answer

1 like 0 dislike
by 39 42 54
 
Best answer

Get Drive Disk Information using 'C#'

To get information about a custom drive disk, including details like TotalSize, AvailableFreeSpace, and BusySpace programmatically using C# , you should follow the below steps in details:

Steps

1) Develop a Configuration class to centralize the collection of all application configurations

       public class Configuration
       {
       }

2) Create a readonly properity it's type DriveInfo with namespace

       private readonly DriveInfo _drive;

3) Create constructor that inject DriveInfo

      public Configuration(DriveInfo drive)
      {
           _drive = drive;
      }

4) Create function with name : DriveSize

     public (double, double, double) DriveSize()
     {
     }

5) Within the previous method, we should utilize the _drive object, which contains more detailed drive information .

     double totalSize = _drive.TotalSize; 
     double availableFreeSpace = _drive.AvailableFreeSpace;
     double busySpace = totalSize - availableFreeSpace;
  • _drive.TotalSize : Retrieve the total size for the drive.
  • _drive.AvailableFreeSpace : Retrieve the available Free Space for the drive.

6) Develop a BusinessLogic class to that call configuration class
7) Navigate to Web.Config and add new key " LogPath " with value " E:\Logs "

    <configuration>
	     <appSettings>
		   <!-- This is the path of logs on custom server -->
		   <add key="LogPath" value="E:\Logs" />
	     </appSettings>
    </configuration>

8) Retrieve a value from the Web.Config file.

    static string Location = ConfigurationManager.AppSettings["LogPath"];

9) Instantiate an object from the DriveInfo class within this class, using 'Location' as a parameter

    static DriveInfo drive = new DriveInfo(Location);

10) Call Configuration class to use all contain method such as : DriveSize()

    Configuration conf = new Configuration(drive);

11) Call method using conf.DriveSize()

Code Snippets:

    public class Configuration
    { 
        private readonly DriveInfo _drive;

        public Configuration(DriveInfo drive)
        {
             _drive = drive;
        }
        public (double, double, double) DriveSize()
        {
             //string driveName = drive.Name;
             if (!_drive.IsReady)
             {
                  throw new DriveNotFoundException();
             }
             double totalSize = _drive.TotalSize;
             double availableFreeSpace = _drive.AvailableFreeSpace;
             double busySpace = totalSize - availableFreeSpace;

             return (totalSize, availableFreeSpace, busySpace);
      }
   }
If you don’t ask, the answer is always NO!
...