开发者

Get the path of a file dragged into a Windows Forms form

开发者 https://www.devze.com 2023-01-28 08:03 出处:网络
I am developing an application which requires the user t开发者_StackOverflowo drag a file from Windows Explorer into the application window (Windows Forms form). Is there a way to read the file n

I am developing an application which requires the user t开发者_StackOverflowo drag a file from Windows Explorer into the application window (Windows Forms form). Is there a way to read the file name, path and other properties of the file in C#?


You can catch the DragDrop event and get the files from there. Something like:

void Form_DragDrop(object sender, DragEventArgs e)
{
    string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

    //more processing
}


you Should Use Two Events 1) DragDrop 2) DragEnter

Also enable "AllowDrop" Property of Panel/form to true.

 private void form_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    private void form_DragDrop(object sender, DragEventArgs e)
    {
        string[] filePaths= (string[])e.Data.GetData(DataFormats.FileDrop, false);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消