question

Prabs-7622 avatar image
0 Votes"
Prabs-7622 asked TimonYang-MSFT commented

How to check avilable free disk space drive on linux using C#.Net Core

Hi,
I have an console application which is developed with .net core 3.1.
I want to check free available disk space drive on linux.
Please help me on this.
In windows it will look like this, but i need to check on linux

         string maxFreeSpaceDrive = string.Empty;
         long maxFreeSpace = 0;
         try
         {
             DriveInfo[] allDrives = DriveInfo.GetDrives();
             //// Get the max size drive
             foreach (DriveInfo drive in allDrives)
             {
                 try
                 {
                     if (drive.IsReady && drive.DriveType == DriveType.Fixed)
                     {
                         if (drive.TotalFreeSpace > maxFreeSpace)
                         {
                             maxFreeSpace = drive.TotalFreeSpace;
                             maxFreeSpaceDrive = drive.Name;
                         }
                     }
                 }
                 catch (IOException)
                 {
                 }
             }

             return maxFreeSpaceDrive;
dotnet-csharpdotnet-runtimedotnet-runtime-frameworkdotnet-runtime-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

As mentioned in the source code of DriveInfo, its parameterized structure is suitable for Windows and Unix, I am not sure whether it is suitable for Linux, you can try it.

 var freeBytes = new DriveInfo(path).AvailableFreeSpace; 

Or you can use Process to call the terminal and pass commands like df to get a string like the following, and then process it to get the information you want.

84712-3.png

 public static class ServersManager
 {      
         public static string GetDiskSpace()
         {
             return string.Join(" ", "df").Bash();
         }
    
         private static string Bash(this string cmd)
         {
             var escapedArgs = cmd.Replace("\"", "\\\"");
    
             var process = new Process()
             {
                 StartInfo = new ProcessStartInfo
                 {
                     FileName = "/bin/bash",
                     Arguments = $"-c \"{escapedArgs}\"",
                     RedirectStandardOutput = true,
                     UseShellExecute = false,
                     CreateNoWindow = true,
                 }
             };
             process.Start();
             string result = process.StandardOutput.ReadToEnd();
             process.WaitForExit();
             return result;
         }
 }

The code is excerpted from this link: Finding available space for a directory in C# on Linux
I don't have Linux OS, so I haven't conducted actual tests, you can try to see if it works.

Update:

The above code gets the result of executing certain commands in Linux. If the Linux command is correct, it should be able to get the correct result.

But I don't know much about Linux, which may require you to consult a Linux expert to obtain the appropriate Linux commands.

As for a piece of code that is applicable to both windows and Linux, I have some doubts whether it is possible.

I think it might be better to detect the current OS type first, and then execute different codes:

         public static string GetDiskSpace()
         {
             string diskSpace = string.Empty;
             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
                 diskSpace = GetDiskSpaceInMac();
             }
    
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
             {
                 diskSpace = GetDiskSpaceInLinux();
             }
    
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {
                 diskSpace = GetDiskSpaceInWindows();
             }
    
             return diskSpace;
         }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3.png (21.8 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,
I want to get avilable free disk space drive on linux.
if i run your code, i am getting following details,84846-drive.jpg


0 Votes 0 ·
drive.jpg (96.4 KiB)

Hello, anyone please help me on this,
How to check avilable free disk space drive on linux using C#.Net Core?
The code has to be work on both windows and linux

0 Votes 0 ·

@Prabs-7622
I added some content, please check.

0 Votes 0 ·

Hi, there is no method details about GetDiskSpaceInLinux
Can you please send?

0 Votes 0 ·
Show more comments