파일 등을 드래그 & 드롭으로 가져올 수 있는 코드입니다.

dropFiles 는 해당 파일의 경로를 불러오게 됩니다.

 

파일/폴더를 마우스 드래그로 가져올 수 있습니다.

 

using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            listBox1.AllowDrop = true; //drag & drop 허용
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            //드래그 드롭 파일 (여러 개 가능)
            string[] dropFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
            
            foreach (string file in dropFiles)
            {
                listBox1.Items.Add(file);
            }
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            //마우스 커서 표현
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }
    }
}

+ Recent posts