• C#获取Linux磁盘剩余空间


            /// <summary>
            /// Linux获取磁盘剩余空间,单位byte
            /// </summary>
            /// <param name="path">例:/home</param>
            /// <returns></returns>
            public long LinuxGetFolderDiskInfo(string path)
            {
                try
                {
                    if (string.IsNullOrEmpty(path))
                    {
                        return 0;
                    }
                    if (!path.StartsWith("/"))
                    {
                        path = "/" + path;
                    }
    
                    string shellPathLine = string.Format("cd {0}", path);
                    string shellLine = string.Format("df {0}", path);
    
                    Process p = new Process();
                    p.StartInfo.FileName = "sh";
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    p.StandardInput.WriteLine(shellPathLine);
                    p.StandardInput.WriteLine(shellLine);
                    p.StandardInput.WriteLine("exit");
    
                    string strResult = p.StandardOutput.ReadToEnd();
    
                    string[] arr = strResult.Split('
    ');
                    if (arr.Length == 0)
                    {
                        return 0;
                    }
    
                    string strArray = arr[1].TrimStart().TrimEnd();
    
                    //把字符串中多个空格合并成一个(overlay         20961280 6256512  14704768  30%)
                    while (strArray.IndexOf("  ") > 0)
                    {
                        strArray = strArray.Replace("  ", " ");
                    }
    
                    string[] resultArray = strArray.Split(' ');
                    if (resultArray == null || resultArray.Length == 0)
                    {
                        return 0;
                    }
    
                    //读取可用空间,单位KB
                    long availableSize = Convert.ToInt32(resultArray[3]);
    
                    //KB => byte
                    availableSize *= 1024;
    
                    return availableSize;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
  • 相关阅读:
    mysql 常用命令行
    mysql常用命令
    Mac os安装wget
    linux下给文件夹或者目录赋权
    Python学习相关资料
    Mac常用的一些操作
    Mac os安装git及 git及githup的使用
    Linux磁盘占用100%解决方法
    page-break-after:always
    工具
  • 原文地址:https://www.cnblogs.com/Jackie-sky/p/14026033.html
Copyright © 2020-2023  润新知