[C# 문법] C# 드라이브 용량(Total_Size) 체크 및 남은 용량(Usage) 확인하기


안녕하세요.

 

오늘은 C#으로 드라이브 용량 확인하는 방법에 대해서 알려 드리려고 합니다.

 

한번 익혀 두시면 두고두고 유용하게 사용하실 수 있을 거라 생각합니다!

 

그럼 바로 예제 프로그램을 만들어 보도록 하겠습니다.

 

먼저 빈 윈폼 프로젝트를 생성해 주시기 바랍니다.


 

빈 윈폼 프로젝트 생성 및 컨트롤 배치



저는 위와 같이, 2 개의 ProgressBar 컨트롤과 Label 컨트롤들을 배치하였습니다.

제 컴퓨터에는 C, D 2 개의 드라이브가 있어서 위와 같이 컨트롤을 배치한 겁니다!

 

사용자 환경에 맞춰서 UI 는 언제든지 변할 수 있어요~~


 

그럼 이제 실제 제 컴퓨터의 용량을 확인해 보도록 하겠습니다.


예제 코드


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace Test

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            this.Load += Test_Load;

        }

 

        /// <summary>

        ///  Load 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void Test_Load(object sender, EventArgs e)

        {

            //용량 체크

            GetDriveSize();

        }

 

        /// <summary>

        /// Local 드라이브 찾기

        /// </summary>

        public void GetDriveSize()

        {

            DriveInfo[] drives = DriveInfo.GetDrives();

 

            foreach (DriveInfo drive in drives)

            {

                if (drive.DriveType == DriveType.Fixed)

                {

                    if (drive.Name.Contains("C")) //C 드라이브

                    {

                        SetDriveSize(drive, uiPgb_CDrive, uiLb_CDrive_Title, uiLb_CDrive);

                    }

                    else //D 드라이브

                    {

                        SetDriveSize(drive, uiPgb_DDrive, uiLb_DDrive_Title, uiLb_DDrive);

                    }

                }

            }

        }

 

        /// <summary>

        /// 드라이브 전체 용량사용량남은량 구하기

        /// 컨트롤에  저장

        /// </summary>

        /// <param name="drive"></param>

        public void SetDriveSize(DriveInfo drive, ProgressBar pb, Label title, Label lb)

        {

            string driveName = string.Empty;

            string totalSize = string.Empty;

            string freeSize = string.Empty;

            string usage = string.Empty;

 

            try

            {

                driveName = drive.Name.Substring(01).ToString();

                totalSize = Convert.ToInt32(drive.TotalSize / 

1024 / 1024 / 1024).ToString(); //전체 사이즈

                freeSize = Convert.ToInt32(drive.AvailableFreeSpace 

/ 1024 / 1024 / 1024).ToString(); //남은 사이즈

                usage = (Convert.ToInt32(totalSize) - 

Convert.ToInt32(freeSize)).ToString(); //사용 용량

 

                pb.Maximum = Convert.ToInt32(totalSize);

                pb.Value = Convert.ToInt32(usage);

 

                title.Text = string.Format("Disk ({0}:)", driveName);

                title.AutoSize = true;

 

                lb.Text = string.Format("{0}GB of {1}GB available.", totalSize, freeSize);

                lb.AutoSize = true;

            }

            catch { }

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과




 

위와 같이 제가 만든 드라이브 용량 확인 프로그램이 실제 제 PC의 드라이브 용량과 같이 표시되는 것을 확인하실 수 있습니다.

 

오늘은 간단히 C# 에서 드라이브 용량 확인하는 방법에 대해서 알아보았습니다.

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY