将文件拖到控件中获得文件路径

    文件拖过来的代码为:

(System.Array)e.Data.GetData(DataFormats.FileDrop)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;  

namespace WindowsFormsApplication1
{
    public partial class MD5 : Form
    {
        public MD5()
        {
            InitializeComponent();
       
        }
         private void MD5_Load(object sender, EventArgs e)
        {
            SetCtrlDrag.SetCtrlDragEvent(this.textBox1);
        }
     }
    public class SetCtrlDrag
    {

        public static void SetCtrlDragEvent(Control ctrl)
        {

            if (ctrl is TextBox)
            {

                TextBox tb = ctrl as TextBox;

                tb.AllowDrop = true;

                tb.DragEnter += (sender, e) =>
                {

                    e.Effect = DragDropEffects.Link;//拖动时的图标

                };

                tb.DragDrop += (sender, e) =>
                {

                    ((TextBox)sender).Text = "文件路径:"+((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

                };

            }

        }

    }
 }

 

posted on 2015-03-11 14:20  陌上清欢  阅读(143)  评论(1)    收藏  举报

导航