照着书敲的。留作笔记吧。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace _18获取本机磁盘信息 { class Program { static void Main(string[] args) { GetDiskInfo(); Console.ReadKey(); } [DllImport("kernel32.dll",EntryPoint="GetDiskFreeSpaceEx")] public static extern int GetDiskFreeSpaceEx(string lpDirectory, out long lpFreeBytesAvailable, out long lpTotalNumberOfBytes, out long lpTotalNumberOfFreeBytes); public static void GetDiskInfo() { long fb, ftb, tfb; string path = Console.ReadLine(); if(GetDiskFreeSpaceEx(path, out fb, out ftb, out tfb) != 0) { string strFb = Convert.ToInt32(fb / 1024 /1024) + "M"; string strFtb = Convert.ToInt32(ftb / 1024 / 1024) + "M"; string strTfb = Convert.ToInt32(tfb / 1024 / 1024) + "M"; Console.WriteLine("总空间:{0} 可用空间:{1} 总剩余空间:{2}",strFb, strFtb, strTfb); } } } }