C#实现文件拖入的效果,类似记事本文件拖入记事本。

来源:互联网 发布:淘宝2016双十一 编辑:程序博客网 时间:2024/06/06 17:52
假设窗体中有一个TextBox名为textBox1。1、把textBox1的AllowDrop属性设置为true2、添加textBox1的DragEnter事件处理过程:private void textBox1_DragEnter(object sender, DragEventArgs e)        {            if (e.Data.GetDataPresent(DataFormats.FileDrop))            {                e.Effect = DragDropEffects.All;            }            else            {                e.Effect = DragDropEffects.None;            }        }3、添加textBox1的DragDrop事件处理过程:private void textBox1_DragDrop(object sender, DragEventArgs e)        {            string fname = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();            System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.Open, System.IO.FileAccess.Read);            byte[] bytes = new byte[fs.Length];            fs.Read(bytes, 0, (int)fs.Length);            textBox1.Text = Encoding.Default.GetString(bytes);        }
原创粉丝点击