• 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;
                }
            }
  • 相关阅读:
    SignalR的三个Demo
    SignalR的一点点东西
    如何在appconfig中配置服务的ip
    IP分片丢失重传
    以太网之物理层
    以太网数据格式与封装解封
    OSI七层模型与TCP/IP五层模型
    边沿检测方法-FPGA入门教程
    如何用ModelsimSE仿真IP核-以PLL为例
    搭建Modelsim SE仿真环境-使用do文件仿真
  • 原文地址:https://www.cnblogs.com/Jackie-sky/p/14026033.html
Copyright © 2020-2023  润新知