티스토리 뷰


윈폼 개발중 많이 사용하는 일이


사용자가 파일브라우저에서 드래그 앤 드랍하여 파일을 인서트 시키는 일입니다.



아래의 간단한 코드로 구현할 수 있습니다.



        public Form1()

        {

            InitializeComponent();


            this.AllowDrop = true;

            this.DragEnter += new DragEventHandler(Form1_DragEnter);

            this.DragDrop += new DragEventHandler(Form1_DragDrop);

        }


        void Form1_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;

        }


        void Form1_DragDrop(object sender, DragEventArgs e)

        {

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (string file in files) Console.WriteLine(file);

        }

댓글