1.第一种使用shell命令实现:

 private DiskInfo LinuxGetFolderDiskInfo(string path)
{
DiskInfo disk = new DiskInfo();
if (string.IsNullOrEmpty(path))
{
return disk;
}
if (!path.StartsWith("/"))
{
path = "/" + path;
} string shellPathLine = string.Format("cd {0}", path);
string printLine = " awk '{print $2,$3,$4,$5}'";
string shellLine = string.Format("df -k {0} |", path) + printLine;
logger.Debug(string.Format("执行命令:{0}", shellLine)); 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();
logger.Debug(string.Format("输出结果:{0}", strResult));
string[] arr = strResult.Split('\n');
if (arr.Length==)
{
return disk;
}
string[] resultArray = arr[].TrimStart().TrimEnd().Split(' ');
if (resultArray==null || resultArray.Length == )
{
return disk;
} disk.TotalSize = Convert.ToInt32(resultArray[]);
disk.UsedSize = Convert.ToInt32(resultArray[]);
disk.AvailableSize = Convert.ToInt32(resultArray[]);
disk.Use = resultArray[];
logger.Debug(string.Format("Linux获取目录:{0},总大小:{1},已用:{2},未用:{3},使用率:{4}", path, disk.TotalSize, disk.UsedSize, disk.AvailableSize, disk.Use)); return disk;
}

2.第二种使用statvfs()函数实现:(需要下载Mono.Posix.dll,引用using Mono.Unix.Native;)

 public DiskInfo LinuxGetFolderDiskInfo(string path)
{
DiskInfo disk = new DiskInfo();
try
{
if (string.IsNullOrEmpty(path))
{
return disk;
}
if (!path.StartsWith("/"))
{
path = "/" + path;
} Statvfs stavfs;
Syscall.statvfs(path, out stavfs);
disk.TotalSize = (long)(stavfs.f_blocks * stavfs.f_bsize);
disk.AvailableSize = (long)(stavfs.f_bavail * stavfs.f_bsize);
disk.UsedSize = (long)(stavfs.f_bfree * stavfs.f_bsize); logger.Debug(string.Format("Linux获取目录:{0},总大小:{1},已用:{2},未用:{3}", path, disk.TotalSize, disk.UsedSize, disk.AvailableSize));
return disk;
}
catch (Exception ex)
{
logger.Error(ex);
return disk;
}
}
 public class DiskInfo
{
public long TotalSize { get; set; } public long UsedSize { get; set; } public long AvailableSize { get; set; } public string Use { get; set; }
}

C#在Linux下获取文件夹信息(所在磁盘总大小,使用空间,已用空间,使用率)的更多相关文章

  1. linux下各文件夹的结构说明及用途介绍

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令.   /dev:设备特殊文件.   /etc:系统管理和配置文件.   /etc/rc.d:启动的配 置文件和脚本.   /ho ...

  2. 【转】linux下各文件夹的结构说明及用途介绍

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令. /dev:设备特殊文件. /etc:系统管理和配置文件. /etc/rc.d:启动的配 置文件和脚本. /home:用户主目录 ...

  3. Linux下各文件夹的结构说明及用途介绍(转载)

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令. /dev:设备特殊文件. /etc:系统管理和配置文件. /etc/rc.d:启动的配 置文件和脚本. /home:用户主目录 ...

  4. 【转载】Linux下各文件夹的含义和用途

    原文地址:https://www.cnblogs.com/lanqingzhou/p/8037269.html Linux下各文件夹的含义和用途 Linux根目录”/“下各个系统文件夹的含义和用途 1 ...

  5. linux下各文件夹的结构说明及用途介绍:Linux目录结构介绍

    linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令. /dev:设备特殊文件. /etc:系统管理和配置文件. /etc/rc.d:启动的配 置文件和脚本. /home:用户主目录 ...

  6. Web 在线文件管理器学习笔记与总结(11)获取文件夹信息 (12)返回上一级操作

    (11)获取文件夹信息 文件夹没有修改操作. index.php: <?php require 'dir.func.php'; require 'file.func.php'; require ...

  7. .net core在Linux下获取AD域信息

    .net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...

  8. Linux下的文件夹创建命令使用实践

    [文章摘要] 本文以实际的C源程序为样例,介绍了Linux下的文件夹创建命令(mkdir)的用法.为相关开发工作的开展提供了故意的參考. [关键词] C语言  Linux  文件夹创建  makefi ...

  9. cocos2d-x3.2下获取文件夹下所有文件名的方法

    这里提供一个函数获取文件夹下所有文件名的方法,直接上代码了. 原文地址:http://blog.csdn.net/qqmcy/article/details/36184733 // //  Visib ...

随机推荐

  1. vue2.0学习——使用webstorm创建一个vue项目

    https://blog.csdn.net/weixin_40877388/article/details/80911934

  2. lua之链表的实现

    -- lua链表的实现 node = {} list = node --初始化,构建一个空表 function init() list.data = --我将头结点的数据域存放链表的长度,以免浪费空间 ...

  3. mac vim编辑器常用操作快捷方式

    0 行首$ (shift+6)行尾gg 文首G(shift+g) 文尾A(Shift+a)文尾,并编辑ctrl+f 向上翻页ctrl+b 向下翻页ctrl+u 向上翻半页ctrl+d 向下翻半页数字+ ...

  4. HttpClient 学习整理 (转)

    source:http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能 ...

  5. vue 清除keep-Alive页面缓存

  6. CF1065D Three Pieces

    题目描述:给出一个n*n的棋盘,棋盘上每个格子有一个值.你有一个子,要求将这个子从1移到n*n(去k时可以经过比k大的点). 开局时它可以作为车,马,相(国际象棋).每走一步耗费时间1.你也可以中途将 ...

  7. jQuery的鼠标移入与移出事件

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  8. LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Mysql篇

    文章来源:http://www.cnblogs.com/hello-tl/p/7569097.html 更新时间:2017-09-21 16:06 简介 LAMP+R指Linux+Apache+Mys ...

  9. leetcode-169求众数

    求众数 思路: 记录每个元素出现的次数,然后查找到众数 代码: class Solution: def majorityElement(self, nums: List[int]) -> int ...

  10. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...