■ DriveInfo 클래스 : 디스크 드라이브 정보 구하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.IO;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                                    += Form_Load;

            this.driveComboBox.SelectedIndexChanged += driveComboBox_SelectedIndexChanged;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            foreach(DriveInfo driveInfo in DriveInfo.GetDrives())

            {

                this.driveComboBox.Items.Add(driveInfo.Name);

 

                this.driveComboBox.SelectedIndex = 0;

            }

        }

 

        #endregion

        #region 드라이브 콤보 박스 선택 인덱스 변경시 처리하기 - driveComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 드라이브 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void driveComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            string driveLetter = this.driveComboBox.Text.Substring(0, 1);

 

            DriveInfo driveInfo = new DriveInfo(driveLetter);

            

            if(driveInfo.IsReady)

            {

                this.nameValueLabel.Text               = driveInfo.Name;

                this.totalSizeValueLabel.Text          = driveInfo.TotalSize.ToString();

                this.totalFreeSizeValueLabel.Text      = driveInfo.TotalFreeSpace.ToString();

                this.availableFreeSpaceValueLabel.Text = driveInfo.AvailableFreeSpace.ToString();

                this.formatValueLabel.Text             = driveInfo.DriveFormat;

                this.typeValueLabel.Text               = driveInfo.DriveType.ToString();

                this.isReadyValueLabel.Text            = driveInfo.IsReady.ToString();

                this.rootDirectoryValueLabel.Text      = driveInfo.RootDirectory.Name;

                this.volumeLabelValueLabel.Text        = driveInfo.VolumeLabel;

            }

            else

            {

                this.nameValueLabel.Text               = driveInfo.Name;

                this.totalSizeValueLabel.Text          = string.Empty;

                this.totalFreeSizeValueLabel.Text      = string.Empty;

                this.availableFreeSpaceValueLabel.Text = string.Empty;

                this.formatValueLabel.Text             = string.Empty;

                this.typeValueLabel.Text               = driveInfo.DriveType.ToString();

                this.isReadyValueLabel.Text            = driveInfo.IsReady.ToString();

                this.rootDirectoryValueLabel.Text      = driveInfo.RootDirectory.Name;

                this.volumeLabelValueLabel.Text        = string.Empty;

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요