//program.cs using System; using System.Collections.Generic; using System.Windows.Forms; namespace DriveInfo_GetDrives { static class Program { /// <summary> /// 應用程式的主進入點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } //form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace DriveInfo_GetDrives { public partial class Form1 : Form { private long m = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void Button_GetInfo_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder();//建立stringbuilder對象,用於在記憶體中儲存臨時資訊 DriveInfo[] myAllDrives = DriveInfo.GetDrives();// DriveInfo對象數組,儲存獲得的磁碟資訊 try { foreach (DriveInfo myDrive in myAllDrives)//迴圈寫入每個磁碟的參數 屬性 { if (myDrive.IsReady) { sb.Append("磁碟機盤符:"); sb.AppendLine(myDrive.Name); sb.Append("磁碟卷標:"); sb.AppendLine(myDrive.VolumeLabel); sb.Append("磁碟類型:"); sb.AppendLine(myDrive.DriveType.ToString()); sb.Append("磁碟格式:"); sb.AppendLine(myDrive.DriveFormat); sb.Append("磁碟大小:"); m = myDrive.TotalSize / (1024 * 1024); sb.AppendLine(m.ToString()+"M"); sb.Append("磁碟可用剩餘空間:"); m = myDrive.AvailableFreeSpace / (1024 * 1024); sb.AppendLine(m.ToString()+"M"); sb.Append("磁碟總剩餘空間:"); m= myDrive.TotalFreeSpace/(1024*1024); sb.AppendLine(m.ToString()+"M"); sb.AppendLine("--------------------------------------------------"); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } this.textBox_ShowInfo.Text = sb.ToString();//顯示資訊 } } } |