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

Prabs 1 Reputation point
2021-04-05T07:11:19.157+00:00

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;
Developer technologies | .NET | .NET Runtime
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-04-06T02:32:48.207+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.