Form的拖拽事件, private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            { e.Effect = DragDropEffects.None; }
        }        private void Form1_DragDrop(object sender, DragEventArgs e)
        {...}
如何判断拖拽进去的是“文件”还是“文件夹”?
如果是文件,列出拖进去的所有文件的文件路径;
如果是文件夹,列出全部子目录及子目录下的文件路径。

解决方案 »

  1.   

    http://topic.csdn.net/u/20080306/15/2532d0a0-2c72-46d2-9773-1ab6c79391f2.html
      

  2.   

    拖拽实现
    http://blog.csdn.net/Sugar_Tiger/archive/2008/10/23/3131691.aspx   //可以用下面的方法,如果取出来后缀名为空就判断为文件夹
                string strSuffix = System.IO.Path.GetExtension(文件路径);
                if (string.IsNullOrEmpty(strSuffix))
                {
                    //列出此路径下所有文件
                }
                else
                {
                    //其它操作
                }
      

  3.   

    二楼的代码我觉得有点小毛病,因为存在一些没有扩展名的文件。
            private void Form1_DragDrop(object sender, DragEventArgs e)
            {
                string data = e.Data.GetData(DataFormats.FileDrop) as string;
                if (data != null)
                {
                    if (File.Exists(data))
                    {
                        //data表示拖入的文件
                    }
                    else
                    { 
                        //data表示拖入的文件夹
                        //下面在对data递归就可以了
                    }
                }
            }
      

  4.   

    写了个完整代码,可以拖拽:文件、目录、多个文件、多个目录及混合部分
    Form中加个textBox1
    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Multiline = true;
        textBox1.AllowDrop = true;
        this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);
        this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop);
    }
    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
        else e.Effect = DragDropEffects.None;
    }
    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop); // 可能是多个文件和目录
            foreach (string path in paths)
            {
                // MessageBox.Show(path);
                if (Directory.Exists(path))
                {
                    al.Add(path + "\\\r\n");
                    GetAllDirList(path);
                }
                else
                {
                    al.Add(path + "\r\n");
                }
            }
            for (int i = 0; i < al.Count; i++)
            {
                textBox1.AppendText(al[i].ToString());
            }
        }
    }public ArrayList al = new ArrayList();
    public void GetAllDirList(string strBaseDir)
    {
        DirectoryInfo di = new DirectoryInfo(strBaseDir);
        foreach (DirectoryInfo sdi in di.GetDirectories())
        {
            al.Add(sdi.FullName + "\\\r\n");
            GetAllDirList(sdi.FullName);   //递归
        }
        foreach (FileInfo fi in di.GetFiles())
        {
            al.Add(fi.FullName + "\r\n");
        }
    }
      

  5.   

    8楼代码vs2005+XP测试通过.
    e.Data.GetDataPresent(DataFormats.FileDrop  // 判断是否为文件或目录if (Directory.Exists(path))          // 判断是否为目录,不是目录即为文件GetAllDirList(string strBaseDir)             // 递归查找子目录及文件