c# 文件系统

c# 复制代码
using System.Runtime.InteropServices;
using static DiskInfo.FileSystemInfo;

namespace DiskInfo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                try
                {
                    string path = args.Length > 0 ? args[0] : "."; // 默认当前目录
                    FileSystemStats info = GetFileSystemInfo(path);

                    Console.WriteLine($"tsize: {info.tsize}");
                    Console.WriteLine($"bsize: {info.bsize}");
                    Console.WriteLine($"blocks: {info.blocks}");
                    Console.WriteLine($"bfree: {info.bfree}");
                    Console.WriteLine($"bavail: {info.bavail}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("end");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }

    class FileSystemInfo
    {
        // 定义结构体
        public class FileSystemStats
        {
            public uint tsize;  // 最佳传输大小
            public uint bsize;  // 文件系统块大小
            public ulong blocks; // 总块数
            public ulong bfree;  // 空闲块数
            public ulong bavail; // 可用块数(非特权用户)
        }

        // Windows API
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool GetDiskFreeSpaceEx(
            string lpDirectoryName,
            out ulong lpFreeBytesAvailable,
            out ulong lpTotalNumberOfBytes,
            out ulong lpTotalNumberOfFreeBytes);

        // Linux API
        [DllImport("libc", SetLastError = true)]
        private static extern int statvfs(string path, out StatVfs buf);

        private class StatVfs
        {
            public ulong f_bsize;  // 文件系统块大小
            public ulong f_frsize; // 片段大小
            public ulong f_blocks; // 总块数
            public ulong f_bfree;  // 空闲块数
            public ulong f_bavail; // 可用块数(非特权用户)
            public ulong f_files;  // 总文件节点数
            public ulong f_ffree;  // 空闲文件节点数
            public ulong f_favail; // 可用文件节点数(非特权用户)
            public ulong f_fsid;   // 文件系统 ID
            public ulong f_flag;   // 挂载标志
            public ulong f_namemax;// 最大文件名长度
        }

        // 获取文件系统信息
        public static FileSystemStats GetFileSystemInfo(string path)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return GetWindowsFileSystemInfo(path);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return GetLinuxFileSystemInfo(path);
            }
            else
            {
                throw new PlatformNotSupportedException("Unsupported platform.");
            }
        }

        // 获取 Windows 文件系统信息
        private static FileSystemStats GetWindowsFileSystemInfo(string path)
        {
            if (!GetDiskFreeSpaceEx(path, out ulong freeBytesAvailable, out ulong totalNumberOfBytes, out ulong totalNumberOfFreeBytes))
            {
                throw new Exception("Failed to get disk space information.");
            }

            var info = new FileSystemStats
            {
                tsize = 4096, // 假设最佳传输大小为 4KB
                bsize = 4096, // 假设块大小为 4KB
                blocks = totalNumberOfBytes / 4096,
                bfree = totalNumberOfFreeBytes / 4096,
                bavail = freeBytesAvailable / 4096
            };

            return info;
        }

        // 获取 Linux 文件系统信息
        private static FileSystemStats GetLinuxFileSystemInfo(string path)
        {
            if (statvfs(path, out StatVfs buf) != 0)
            {
                throw new Exception("Failed to get filesystem information.");
            }

            var info = new FileSystemStats
            {
                tsize = (uint)buf.f_bsize, // 最佳传输大小
                bsize = (uint)buf.f_bsize, // 块大小
                blocks = buf.f_blocks,     // 总块数
                bfree = buf.f_bfree,       // 空闲块数
                bavail = buf.f_bavail      // 可用块数(非特权用户)
            };

            return info;
        }
    }
}
相关推荐
是发财不是旺财7 分钟前
跟着deepseek学golang--认识golang
开发语言·后端·golang
我的golang之路果然有问题7 分钟前
快速上手GO的net/http包,个人学习笔记
笔记·后端·学习·http·golang·go·net
Apifox.9 分钟前
Apifox 4月更新|Apifox在线文档支持LLMs.txt、评论支持使用@提及成员、支持为团队配置「IP 允许访问名单」
前端·人工智能·后端·ai·ai编程
BXCQ_xuan1 小时前
基于Node.js的健身会员管理系统的后端开发实践
后端·mysql·node.js
拉满buff搞代码1 小时前
搞定 PDF“膨胀”难题:Python + Java 的超实用压缩秘籍
后端
FAQEW1 小时前
Spring boot 中的IOC容器对Bean的管理
java·spring boot·后端·bean·ioc容器
<<1 小时前
基于Django的权限管理平台
后端·python·django
林夕11201 小时前
颠覆认知的MySQL全解析:安装、连接到SQL三大核心语句全掌握
后端·mysql
Java中文社群3 小时前
最火向量数据库Milvus安装使用一条龙!
java·人工智能·后端
JAVA百练成神3 小时前
深度理解spring——BeanFactory的实现
java·后端·spring