간단한 파일 탐색기를 구현했다.
Drag And Drop도 구현
Ctrl+마우스 DnD : 복사, 마우스 DnD : 이동


탐색기 갱신의 약간의 버그와 소스코드가 드러운데
나중을 위해서 그냥 저장... 


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 TestExplorer
{
    public partial class MainForm : Form
    {
        private TreeNode sourceNode = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void LoadDrive()
        {
            DriveInfo[] drivers = DriveInfo.GetDrives();
            foreach (DriveInfo i in drivers)
            {
                if ( (i.DriveType & DriveType.Fixed) == DriveType.Fixed )
                {
                    TreeNode newNode1 = this.treeView1.Nodes.Add(i.Name);
                    TreeNode newNode2 = this.treeView2.Nodes.Add(i.Name);
                    DirectoryInfo di = new DirectoryInfo(i.Name);
                    try
                    {
                        if (di.GetFileSystemInfos().Count() > 0)
                        {
                            newNode1.Nodes.Add("temp");
                            newNode2.Nodes.Add("temp");
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
        }

        private void RefreshNode(TreeNode sourceNode, TreeNode targetNode)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDrive();
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            // root는 제외
            //MessageBox.Show(e.Node.Text);
            e.Node.Nodes.Clear();
            string fullPath = e.Node.FullPath;
            DirectoryInfo di = new DirectoryInfo(fullPath);
            FileSystemInfo[] arrFsi = di.GetFileSystemInfos();
            e.Node.TreeView.BeginUpdate();
            foreach (FileSystemInfo i in arrFsi)
            {
                if ( (i.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden )
                {
                    TreeNode newNode = e.Node.Nodes.Add(i.Name);
                    DirectoryInfo newDI = new DirectoryInfo(i.FullName);
                    try
                    {
                        if (newDI.GetFileSystemInfos().Count() > 0)
                        {
                            newNode.Nodes.Add("temp");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            e.Node.TreeView.EndUpdate();
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            System.Console.WriteLine("treeView1_MouseDown");
            TreeViewHitTestInfo hitInfo = this.treeView1.HitTest(e.Location);
            if (hitInfo != null && hitInfo.Node != null)
            {
                TreeNode node = hitInfo.Node;
                System.Console.WriteLine(node.FullPath);

                // 현재 노드를 선택해준다.
                node.TreeView.SelectedNode = node;
            }
        }

        private void treeView1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            System.Console.WriteLine("treeView1_QueryContinueDrag");
            if (e.EscapePressed)
            {
                e.Action = DragAction.Cancel;
            }
        }

        private void treeView2_DragEnter(object sender, DragEventArgs e)
        {
            System.Console.WriteLine("treeView2_DragEnter");
        }

        private void treeView2_DragOver(object sender, DragEventArgs e)
        {
            System.Console.WriteLine("treeView2_DragOver");
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                if ((e.KeyState & 8) == 8)
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.Move;
                }
            }
        }

        private void treeView2_DragDrop(object sender, DragEventArgs e)
        {
            System.Console.WriteLine("treeView2_DragDrop");
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string sourcePath = (string)e.Data.GetData(DataFormats.StringFormat);
                Point pt = new Point(MousePosition.X, MousePosition.Y);
                pt = this.treeView2.PointToClient(pt);
                TreeViewHitTestInfo hitInfo = this.treeView2.HitTest(pt);
                if (hitInfo != null && hitInfo.Node != null)
                {
                    string targetPath = hitInfo.Node.FullPath;

                    // 폴더 복사
                    if (Directory.Exists(sourcePath) == true
                         && Directory.Exists(targetPath) == true)
                    {
                        FileProgressForm form = new FileProgressForm();
                        form.SourcePath = sourcePath;
                        form.TargetPath = targetPath;

                        if (e.Effect == DragDropEffects.Move)
                        {
                            form.Action = FileProgressForm.ACTION_TYPE.MOVE;
                        }
                        else if (e.Effect == DragDropEffects.Copy)
                        {
                            form.Action = FileProgressForm.ACTION_TYPE.COPY;
                        }
                        DialogResult result = form.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            TreeView tree = hitInfo.Node.TreeView;
                            hitInfo.Node.TreeView.TopNode.Collapse();
                            hitInfo.Node.TreeView.TopNode.Expand();
                            hitInfo.Node.Collapse();
                            hitInfo.Node.Expand();
                            sourceNode.TreeView.TopNode.Collapse();
                            sourceNode.TreeView.TopNode.Expand();
                            sourceNode.Collapse();
                            sourceNode.Expand();
                        }
                        else if (result == DialogResult.Cancel)
                        {
                        }
                    }
                    // 파일 복사
                    else if (File.Exists(sourcePath) == true
                        && Directory.Exists(targetPath) == true)
                    {
                        FileProgressForm form = new FileProgressForm();
                        form.SourcePath = sourcePath;
                        form.TargetPath = targetPath;

                        if (e.Effect == DragDropEffects.Move)
                        {
                            form.Action = FileProgressForm.ACTION_TYPE.MOVE;
                        }
                        else if (e.Effect == DragDropEffects.Copy)
                        {
                            form.Action = FileProgressForm.ACTION_TYPE.COPY;
                        }
                        DialogResult result = form.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            TreeView tree = hitInfo.Node.TreeView;
                            hitInfo.Node.TreeView.TopNode.Collapse();
                            hitInfo.Node.TreeView.TopNode.Expand();
                            hitInfo.Node.Collapse();
                            hitInfo.Node.Expand();
                            sourceNode.TreeView.TopNode.Collapse();
                            sourceNode.TreeView.TopNode.Expand();
                            sourceNode.Collapse();
                            sourceNode.Expand();
                        }
                        else if (result == DialogResult.Cancel)
                        {
                        }
                    }
                    else
                    {
                        MessageBox.Show("파일이 존재하지 않거나, 복사하려는 곳이 폴더가 아닙니다.");
                    }
                }
            }
        }

        private void Main_Shown(object sender, EventArgs e)
        {
            //DialogResult result = MessageBox.Show("정식 버전을 구입하세요", "확인", MessageBoxButtons.YesNo);

            //if (result == DialogResult.No)
            //{
            //    this.Close();
            //}
            //else
            //{
            //    MessageBox.Show("정식 버전을 구입해 주셔서 감사합니다.");
            //}
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode node = e.Item as TreeNode;
            if (node != null )
            {
                System.Console.WriteLine(node.FullPath);

                string fullPath = node.FullPath;
                sourceNode = node;
                Console.WriteLine("treeView1_ItemDrag=" + fullPath);
                // DoDragDrop 시작
                DragDropEffects effects = DoDragDrop(fullPath, DragDropEffects.Copy | DragDropEffects.Move);

                // 트리 부모 노드를 갱신해 준다.
                if (effects == DragDropEffects.Move)
                {
                }
            }
        }

        private void treeView2_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
        {
        }
    }
}

designer code 
namespace TestExplorer
{
    partial class MainForm
    {
        /// 

        /// 필수 디자이너 변수입니다.
        /// 

        private System.ComponentModel.IContainer components = null;

        /// 

        /// 사용 중인 모든 리소스를 정리합니다.
        /// 

        /// 관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 디자이너에서 생성한 코드

        /// 

        /// 디자이너 지원에 필요한 메서드입니다.
        /// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
        /// 

        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.treeView2 = new System.Windows.Forms.TreeView();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.splitContainer1.Location = new System.Drawing.Point(13, 13);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.treeView2);
            this.splitContainer1.Size = new System.Drawing.Size(659, 387);
            this.splitContainer1.SplitterDistance = 327;
            this.splitContainer1.TabIndex = 2;
            // 
            // treeView1
            // 
            this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.treeView1.HotTracking = true;
            this.treeView1.Location = new System.Drawing.Point(3, 3);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(321, 381);
            this.treeView1.TabIndex = 4;
            this.treeView1.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.treeView1_QueryContinueDrag);
            this.treeView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
            this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
            this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
            // 
            // treeView2
            // 
            this.treeView2.AllowDrop = true;
            this.treeView2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.treeView2.HotTracking = true;
            this.treeView2.Location = new System.Drawing.Point(3, 3);
            this.treeView2.Name = "treeView2";
            this.treeView2.Size = new System.Drawing.Size(322, 381);
            this.treeView2.TabIndex = 3;
            this.treeView2.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
            this.treeView2.NodeMouseHover += new System.Windows.Forms.TreeNodeMouseHoverEventHandler(this.treeView2_NodeMouseHover);
            this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView2_DragDrop);
            this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView2_DragEnter);
            this.treeView2.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView2_DragOver);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(684, 412);
            this.Controls.Add(this.splitContainer1);
            this.Name = "MainForm";
            this.Text = "TestExplorer";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Shown += new System.EventHandler(this.Main_Shown);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.TreeView treeView2;
        private System.Windows.Forms.TreeView treeView1;

    }
}


posted by 뚱2