比如 我现在winform上有一个button  我想用户可以在本机上任意选择一个文件。。或者文件交 拖到button上 然后我可以获得该文件的 文件名文件路径谢谢大家 帮着看看 可以 实现吗?

解决方案 »

  1.   

    下载我的记事本,里面有例子。
    http://d.download.csdn.net/down/2097063/wuyazhe
      

  2.   


            //实现文件拖放计算
            private void Form1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    e.Effect = DragDropEffects.Copy;
                else 
                    e.Effect = DragDropEffects.None;        }        private void Form1_DragDrop(object sender, DragEventArgs e)
            {
                tabControl1.SelectedIndex = 0;
                textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//文件路径+文件名
                textBox2.Text = MD5.GetFileMD5(textBox1.Text);
                textBox2.Enabled = true;
                textBox2.Focus();
            }
      

  3.   

    ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();恩 这个是获得文件路径的 我现在想判断 该文件 是什么类型的。。除了 用后缀名的方式。。还有 一种 怎么判断来着?
      

  4.   


    //view.AllowDrop = true;----------------------------------------
          private void view_DragEnter(DragEventArgs drgevent)
          {
            if (m_allowdrop)
            {
              if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
              {
                drgevent.Effect = DragDropEffects.Link;
              }
              else
              {
                drgevent.Effect = DragDropEffects.None;
              }
            }
          }      private void view_DragDrop(DragEventArgs drgevent)
          {
            Array arrayFileName = drgevent.Data.GetData(DataFormats.FileDrop) as Array;
            if (arrayFileName != null)
            {
              foreach (string name in arrayFileName)
              {
                //AddFileNameCtl(name);
              }
            }
          }
      

  5.   

    根据字节码判断
    http://blog.163.com/kke_007/blog/static/171213392009101955036318
    这里有
      

  6.   

    OpenFileDialog dlg=new OpenFileDialog();   
      dlg.Multiselect = true;   
      if(dlg.ShowDialog()==DialogResult.OK)   
      {   
      string[] filesName = dlg.FileNames;  
      foreach (string info in filesName)  
      {  
      File f=new FileInfo(info);  
      }  
      }  
      

  7.   


    扩展名是标示文件类型的一种手段,一般就能够说明问题了,如果用FileInfo是方式,个人感觉不是很适用,因为建立TXT保存空文本,可以存为.js 、.bat 、.C 等文件,但用FileInfo取出来的东西似乎都是一样的
      

  8.   


           #region 文件拖拽
            private void button1_DragDrop(object sender, DragEventArgs e)
            {
                this.textBoxFilePath.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }        private void button1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
            #endregion
      

  9.   

    添加DragEnter,DragDrop事件对拖放的处理
      

  10.   

    同时感谢5楼特别提出 view.AllowDrop = true;
    谢谢。。我一开始就忽略了这点
      

  11.   

    大家 如何判断某文件类型是否为文件夹的呢?  我用后缀的方式 即判断是否含有关键字符.的方式不可行啊 因为有些文件夹的类型就是 xxx.xx.xx的哎呀 我该怎么判断呢??大家帮帮忙。