C# 文件拖拽到文本框获取文件

TextBox    AllowDrop 设置 为true
 
private void txtPhone_DragDrop(object sender, DragEventArgs e)
        {
            string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
 
            if (fileName.Contains("txt") == true)
            {

                this.txtPhone.Text = fileName;
            
            }
            else
            {

                MessageBox.Show("不支持当前拖拽的文件格式,请拖拽文本文件!");
            }

        }

        private void txtPhone_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {

                e.Effect = DragDropEffects.All;
            }

        }

你可能感兴趣的:(C# 文件拖拽到文本框获取文件)