탐색기:

윈폼->TreeView, ListView 생성.

ListView 편집 4 멤버 추가 colName, colSize, colSort, coldModify

InitializeComponent()함수를 통해 컨트롤 생성   Load이벤트 추가.

Load이벤트에서  LoadDirectory()함수호출 :

Directory.GetLogicalDrives(); 함수를 통해 해당

컴퓨터의 "C:\\"형식의 논리 드라이브 이름을 검색.

Foreach문을 통해 해당 논리드라이브안에있는 드라이버 명을

TreeView컨트롤의 트리노드 컬렉션에 추가.  논리 드라이브의 디렉토리만 DirectoryInfo클래스를

하위 디렉토리를 만들고

하위디렉토리가 존재한다면 GetDirectories()함수를 통해 하위디렉토리를 가져와 foreach문에서 하위디렉토리 명을 파라미터로 전달받은 상위 드라이버에 넣는다. 만약에 처음 드라이버 검색을 제외한 디렉토리 검색일때 하위디렉토리를 추가한다.

TreeView 이벤트중 AfterExpand함수를 통해 +버튼을 눌렀을때 발생시키는 이벤트를 추가하고

이벤트안에서 DirectoryInfo클래스를 통해 하위디렉토리를 만들고  AfterExpand함수의 인자인 TreeViewEventArgs 트리뷰 제어 이벤트의 트리노드의 현재할당된 컬렉션을 비운다.

트리뷰의 트리노드와 DirectoryInfo클래스를 통해 만든 하위 디렉토리를 가지와 foreach문에서 하위디렉토리 명을 파라미터로 전달받은 상위 드라이버에 넣는다.

TreeView이벤트인 AfterSelect 이벤트에  폴더(하위디렉터리) 추가하고  ListViewitem클래스를 이용해

리스트 컨트롤항목을 만들고 리스트 컨트롤 항목의 컬렉션에 크기,속성, 수정한 날짜의 string 추가한다.

그후에 폴더(현재디렉터리)정보에 GetFiles()함수를 통해 디렉토리에 있는 파일 정보들을 가지고

ListViewitem클래스를 이용해  리스트 컨트롤항목을 만들고 리스트 컨트롤 항목의 컬렉션에

파일의 크기,파일의 확장자명,수정한날짜를 추가한다.


소스코드:

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
160
161
162
163
164
165
166
167
168
169
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 Spowner.uExplore.Core.FileSystem;
 
namespace SearchTreeView
{
    public partial class Form1 : Form
    {
        private bool firstLoad = false;
 
        public Form1()
        {
            InitializeComponent();
    
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //트리뷰에 local 컴퓨터 디렉토리 정보 출력
                LoadDirectory();
            }
            catch(Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }
        private void LoadDirectory()
        {
            try
            {
                firstLoad = true;
 
                //로컬 정보를 가져와서 노드에 추가한다.
                string[] drivers = Directory.GetLogicalDrives();
                foreach(string drive in drivers)
                {
                    //드라이버 명을 가져온다.
                    TreeNode root = new TreeNode(drive);
                    treeDir.Nodes.Add(root);
 
                    //드라이버 명에해당하는 디렉토리만 찾는다
                    DirectoryInfo dir = new DirectoryInfo(drive);
                    if (dir.Exists)
                    {
                        AddDirectoryNodes(root, dir, false);
                    }
                }
            }
            catch
            {
                throw;
            }
        }
        // TreeView 에서 +,- 버튼으로 확장 축소를 했을때 발생하는 이벤트
       
 
        private void AddDirectoryNodes(TreeNode root, DirectoryInfo dir, bool isLoop)
        {
            try
            {
                DirectoryInfo[] drivers = dir.GetDirectories();
                foreach(DirectoryInfo drive in drivers)
                {
                    //디렉토리명을 파라미터로 전달받은 상위 드라이버를 넣는다.
                    TreeNode childRoot = new TreeNode(drive.Name);
                    root.Nodes.Add(childRoot);
 
                    //처음 드라이버 검색을 제외한 디렉토리 검색일때 하위디렉토리 여부 검색
                    if (isLoop)
                        AddDirectoryNodes(childRoot, drive, false);
                }
            }
            catch(Exception error)
            {
                if(firstLoad==false//!firstLoad ! false일때 참 
                {
                    MessageBox.Show(error.Message);
                }
 
            }
        }
        private void treeDir_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string fullpath = e.Node.FullPath;
 
            try
            {
                listView1.Items.Clear();
                // 폴더를 추가한다.
                DirectoryInfo dir = new DirectoryInfo(fullpath);
                DirectoryInfo[] folders = dir.GetDirectories();
 
                foreach(DirectoryInfo folder in folders)
                {
                    ListViewItem item = new ListViewItem(folder.Name);
                    item.SubItems.Add(""); // 크기, 폴더이므로 없음
                    item.SubItems.Add(folder.Attributes.ToString()); //속성
                    item.SubItems.Add(folder.LastWriteTime.ToString()); // 수정한 날짜
 
                    listView1.Items.Add(item);
                }
                //파일을 추가한다.
                FileInfo[] files = dir.GetFiles();
 
                foreachFileInfo file in files)
                {
                    ListViewItem item = new ListViewItem(file.Name);
 
                    if(file.Length > 1024 * 1024 * 1024)
                    {
                        //지정된 문자열의 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다. 
                        //매개 변수는 문화권 별 서식 지정 정보를 제공합니다.
                        item.SubItems.Add(String.Format("{0}GB", file.Length / 1024 / 1024 / 1024)); 
                    }
                    else if (file.Length > 1024 * 1024)
                    {
                        item.SubItems.Add
                            (String.Format("{0}MB", file.Length / 1024 / 1024));
                    }
                    else if (file.Length > 1024)
                    {
                        item.SubItems.Add
                            (String.Format("{0}KB", file.Length / 1024));
                    }
                    else
                    {
                        item.SubItems.Add
                            (String.Format("{0}KB", 1));
                    }
                    item.SubItems.Add(String.Format("{0}", file.Extension));
                    item.ImageIndex = 2;
                    item.SubItems.Add(file.LastWriteTime.ToString()); //수정한날짜
                    listView1.Items.Add(item);
                }
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
 
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
 
        // +버튼눌렀을때만 발생
        private void treeDir_AfterExpand(object sender, TreeViewEventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(e.Node.FullPath);
            e.Node.Nodes.Clear();
            AddDirectoryNodes(e.Node, dir, true);
 
        }
    }
}
 

'ASP.NET(윈폼)' 카테고리의 다른 글

[ASP.NET] 2.HTML Server Control Class 정리  (0) 2018.08.06
[ASP.NET] 1.Web Form으로 시작하는 ASP.NET  (0) 2018.08.06
[윈폼] 탐색기 추가기능  (1) 2018.07.26
[윈폼] 메모장 추가기능  (0) 2018.07.26
[윈폼] 메모장  (0) 2018.07.24

+ Recent posts