C#之winform实现文件拖拽功能【转】

将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了

将一个控件的属性AllowDrop设置为true,然后添加DragDrop、DragEnter时间处理函数,如下:

private void txtAppPath_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void txtAppPath_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            txtLocalFileName.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        }
private void button1_Click(object sender, EventArgs e)
        {
            // System.Diagnostics.Process.Start("Explorer.exe", "c:\windows");
            System.Diagnostics.Process.Start("Explorer.exe", this.button1.Text);
        }
原文地址:https://www.cnblogs.com/pengchenggang/p/10968216.html