c#.net 2.0 檔案IO之DriveInfo類樣本

來源:互聯網
上載者:User
2008-03-09 22:07

//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();//顯示資訊
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.