본문 바로가기
C#

15. 파일 보기

by Jcoder 2019. 3. 20.



mook_FileView.zip



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; //파일 읽기, 쓰기
using System.Threading; // 스레드
 
namespace mook_FileView
{
    public partial class Form1 : Form
    {
        Thread threadFileView = null// 스레드 개체 생성.
 
        private delegate void OnDelegateFile(string fp, string fn, string fl, string fc);
        private OnDelegateFile OnFile = null// 델리게이트 생성
 
        bool flag = true// 일반 또는 히든 파일 구별
        int DirCount = 0// 폴더 카운트
        int FileCount = 0// 파일 카운트
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            OnFile = new OnDelegateFile(FileResult);
        }
        /*  
            OnFile 델리케이트 대리자에 인자값으로 선언하여 파일의 크기를 구하고, 
            lvFile 컨트롤과 tsslblResult 컨트롤에 파일의 정보를 나타내는 작업
        */
        private void FileResult(string fp, string fn, string fl, string fc)
        {
            string fSize = GetFileSize(Convert.ToDouble(fl)); // 파일의 크기를 구한다.
 
            this.lvFile.Items.Add(new ListViewItem(new string[] { fp, fn, fc, fSize })); //멤버를 추가
 
            this.tsslResult.Text = string.Format($"폴더 : {DirCount} 개, 파일 : {FileCount} 개");
        }
 
        private string GetFileSize(double byteCount)
        {
            string size = "0 Bytes";
 
            if (byteCount >= 1073741824.0)
                size = string.Format("{0:##.##}", byteCount / 1073741824.0+ "GB";
            else if (byteCount >= 1048576.0)
                size = string.Format("{0:##.##}", byteCount / 1048576.0+ "MB";
            else if (byteCount >= 1024.0)
                size = string.Format("{0:##.##}", byteCount / 1024.0+ "KB";
            else if (byteCount > 0 && byteCount < 1024.0)
                size = byteCount.ToString() + "Bytes";
 
            return size;
            throw new NotImplementedException();
        }
 
        private void ItemClear()
        {
            DirCount = 0;
            FileCount = 0;
            this.lvFile.Items.Clear();
        }
 
        private void rbtnAll_CheckedChanged(object sender, EventArgs e)
        {
            ItemClear();
            flag = true// 파일 타입 선별 true = all, false = hidden
            
            if (threadFileView != null)
                threadFileView.Abort(); // threadFileView 스레드 개체가 종료되지 않고 계속 실행중이라면 강제 종료
 
            if (this.txtPath.Text != "")
            {
                this.lvFile.Items.Clear(); // 속성값 초기화
                // threadFileView 스레드 개체를 초기화 하는 문장
                // 파라미터를 사용하는 스레드이기 때문에 Thread(new ParameterizedThreadStart) 형식으로 초기화
                threadFileView = new Thread(new ParameterizedThreadStart(FileView));
                threadFileView.Start(this.fbdFolder.SelectedPath); //threadFileView.Start() 스레드 실행, 인자 값으로 폴더 경로 전송
            }
        }
 
        private void rbtnHidden_CheckedChanged(object sender, EventArgs e)
        {
            ItemClear();
            flag = false// 파일 타입 선별 true = all, false = hidden
 
            if (threadFileView != null)
                threadFileView.Abort(); // threadFileView 스레드 개체가 종료되지 않고 계속 실행중이라면 강제 종료
 
            if (this.txtPath.Text != "")
            {
                this.lvFile.Items.Clear(); // 속성값 초기화
                // threadFileView 스레드 개체를 초기화 하는 문장
                // 파라미터를 사용하는 스레드이기 때문에 Thread(new ParameterizedThreadStart) 형식으로 초기화
                threadFileView = new Thread(new ParameterizedThreadStart(FileView));
                threadFileView.Start(this.fbdFolder.SelectedPath); //threadFileView.Start() 스레드 실행, 인자 값으로 폴더 경로 전송
            }
        }
 
        private void btnPath_Click(object sender, EventArgs e)
        {
            if(this.fbdFolder.ShowDialog() == DialogResult.OK)
            {
                ItemClear();
                this.txtPath.Text = this.fbdFolder.SelectedPath;
 
                threadFileView = new Thread(new ParameterizedThreadStart(FileView));
                threadFileView.Start(this.fbdFolder.SelectedPath); //threadFileView.Start() 스레드 실행, 인자 값으로 폴더 경로 전송
            }
        }
 
        private void FileView(object path)
        {
            DirCount++;
 
            DirectoryInfo di = new DirectoryInfo((string)path); // 개체 di를 생성하는 문장으로 인자값으로 폴더의 경로를 전송
            DirectoryInfo[] dti = di.GetDirectories();  // di 개체에 선언된 폴더 경로의 하위 폴더의 정보를 개체 변수 dti에 저장.
 
            // di 개체에 설정된 폴더의 모든 파일의 정보를 얻는 작업
            foreach (var f in di.GetFiles())
            {
                if (flag == true)   // flag가 true면 전체 파일 검색
                {
                    FileCount++;
                    // OnFile 델리게이트를 호출, 인자 값으로 폴더 이름, 파일 이름, 파일 크기, 마지막 수정시간 사용
                    Invoke(OnFile, f.DirectoryName, f.Name, f.Length.ToString(), f.CreationTime.ToString());
                }
                else   // 숨김 파일 검색
                {
                    // f.Attributes 속성 값이 FileAttributes.Hidden 과 동일할 때 시작.
                    if (f.Attributes.ToString().Contains(FileAttributes.Hidden.ToString()))
                    {
                        FileCount++;
                        Invoke(OnFile, f.DirectoryName, f.Name, f.Length.ToString(), f.CreationTime.ToString());
                    }
                }
            }
 
            // di.GetDirectories()를 이용하여 지정된 폴더에 있는 하위 폴더의 경로를 추출하는 작업.
            // 하위 폴더 경로가 존재하면 수행.
            for (int i = 0; i < di.GetDirectories().Length; i++)
            {
                try
                {
                    FileView(dti[i].FullName);
                }
                catch { continue; }
            }
        }
    }
}
cs


'C#' 카테고리의 다른 글

[C#] CMD 사용  (0) 2020.11.19
[C#] 델파이 dll 호출  (0) 2020.11.19
14. 파일 읽기/쓰기  (0) 2019.03.20
13. 프로세스 보기  (0) 2019.03.20
12. 트레이 메세지  (0) 2019.03.20