프로그램/C# - Study / / 2010. 10. 22. 12:01

C# - Ex04 - 탐색기 만들기

반응형













































































using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ex04
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            TreeNode root = tvFolders.Nodes.Add("내컴퓨터");

            string[] drives = Directory.GetLogicalDrives();
            foreach (string drive in drives)
            {
                TreeNode node = root.Nodes.Add(drive);
                node.Nodes.Add("@%");
            }
        }

        private void tvFolders_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeNode current = e.Node;

            if (current.Nodes.Count == 1 &&
                current.Nodes[0].Text.Equals("@%"))
            {
                current.Nodes.Clear();

                String path = current.FullPath.Substring(
                    current.FullPath.IndexOf("\\") + 1);

                try // 하위 장치(목록)이 없을 경우 예외 처리
                {
                    string[] directories =
                        Directory.GetDirectories(path);
                    foreach (string directory in directories)
                    {
                        TreeNode newNode = current.Nodes.Add(
                            directory.Substring(
                            directory.LastIndexOf("\\") + 1));
                        newNode.Nodes.Add("@%");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {

            TreeNode current = e.Node;
            string path = current.FullPath;

            txtPath.Text =
                path.Substring(path.IndexOf("\\") + 1);

            try
            {
                lvFiles.Items.Clear();

                //디렉토리 목록 표시
                string[] directories =
                    Directory.GetDirectories(txtPath.Text);
                foreach (string directory in directories)
                {
                    DirectoryInfo info =
                        new DirectoryInfo(directory);
                    ListViewItem item =
                        new ListViewItem(new string[]
                    {
                        info.Name, "",
                        "파일폴더", info.LastWriteTime.ToString()
                    });
                    lvFiles.Items.Add(item);
                }


                //파일 목록 표시
                string[] files = Directory.GetFiles(txtPath.Text);

                foreach (string file in files)
                {
                    FileInfo info = new FileInfo(file);
                    ListViewItem item =
                        new ListViewItem(new string[]
                    {
                        info.Name, info.Length.ToString(),
                        info.Extension, info.LastWriteTime.ToString()
                    });
                    lvFiles.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

C# - Ex06 - 그림판  (0) 2010.10.27
C# - Ex05 - 메모장  (0) 2010.10.26
EX13 - Microsoft Enterprise Library 사용해 간단하게 DB 자료 가져오기  (0) 2010.10.21
EX12  (0) 2010.10.21
EX11 - 2  (1) 2010.10.21
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유