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

孤街醉人 提交于 2020-01-23 22:42:42

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

将一个控件的属性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);
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!