글 작성자: 써니루루

C# : Unit Class - File Size 변환

이전에 올렸던 Length 단위에 이어 두번째 File Size 관련된 단위입니다.

귀찮았는데 막상 파일사이즈를 보여야 할 일이 있어서

간단히 Property 를 이용해서 작성해 봤네요.

C# 프로퍼티(Property)의  가장 적절한 예가 아닌가 생각됩니다. ㅋㅋㅋ

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    class FileSize
    {
        public enum UNITS { B, KB, MB, GB, TB }
 
        double b = 0;
        double kb = 0;
        double mb = 0;
        double gb = 0;
        double tb = 0;
 
        public double B
        {
            get
            {
                return this.b;
            }
            set
            {
                this.b = value;
                this.kb = this.b / 1024;
                this.mb = this.kb / 1024;
                this.gb = this.mb / 1024;
                this.tb = this.gb / 1024;
            }
        }
 
        public double KB
        {
            get
            {
                return this.kb;
            }
            set
            {
                this.kb = value;
                this.B = this.kb * 1024;
            }
        }
 
        public double MB
        {
            get
            {
                return this.mb;
            }
            set
            {
                this.mb = value;
                this.KB = this.mb * 1024;
            }
        }
 
        public double GB
        {
            get
            {
                return this.gb;
            }
            set
            {
                this.gb = value;
                this.MB = this.gb * 1024;
            }
        }
 
        public double TB
        {
            get
            {
                return this.tb;
            }
            set
            {
                this.tb = value;
                this.GB = this.tb * 1024;
            }
        }
 
        public FileSize(double size)
        {
            this.B = size;
        }
 
        public override string ToString()
        {
            string ret = string.Empty;
 
            if (b < 1024D)
            {
                ret = string.Format("{0}{1}", b, UNITS.B.ToString());
            }
            else if (kb < 1024D)
            {
                ret = string.Format("{0}{1}", kb, UNITS.KB.ToString());
            }
            else if (mb < 1024D)
            {
                ret = string.Format("{0}{1}", mb, UNITS.MB.ToString());
            }
            else if (gb < 1024D)
            {
                ret = string.Format("{0}{1}", gb, UNITS.GB.ToString());
            }
            else
            {
                ret = string.Format("{0}{1}", tb, UNITS.TB.ToString());
            }
 
            return ret;
        }
    }
 
cs