C# · 12月 20, 2021

C# — 使用 DriveInfo 获取磁盘驱动器信息

C# — 使用 DriveInfo 获取磁盘驱动器信息

1. 代码实现

Main( </span><span style=”color: #0000ff”&gt;private</span> <span style=”color: #0000ff”&gt;static</span> <span style=”color: #0000ff”&gt;void</span><span style=”color: #000000″&gt; GetComputerDisk() { DriveInfo[] LocalDrive </span>=<span style=”color: #000000″&gt; DriveInfo.GetDrives(); </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < LocalDrive.Length; i++<span style=”color: #000000″&gt;) { Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;——————————————</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;驱动器<a href=”https://www.jb51.cc/tag/mingcheng/” target=”_blank” class=”keywords”>名称</a>:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].Name)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;存储空间大小:{0}字节</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].TotalSize)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;可用空间大小:{0}字节</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].AvailableFreeSpace)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;可用空闲空间:{0}字节</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].TotalFreeSpace)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;<a href=”https://www.jb51.cc/tag/wenjian/” target=”_blank” class=”keywords”>文件</a>系统:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].DriveFormat)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;驱动器类型:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].DriveType)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;驱动器IsReady:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].IsReady)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;驱动器的根目录:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].RootDirectory)); Console.WriteLine(</span><span style=”color: #0000ff”&gt;string</span>.Format(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;驱动器卷标:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,LocalDrive[i].VolumeLabel)); } }}</span></pre>

2. 运行结果: