将文件拖到富文本框中以读取文件中的文本

时间:2013-03-11 07:39:38

标签: c# .net drag-and-drop text-files richtextbox

我在将文件拖放到richTextBox时遇到问题,每次将文本文件拖到文件夹上时,它都会变成文本文件的图片,其名称在其下面。双击该文件,它将使用系统默认应用程序打开(即文本文件的记事本等)。基本上它在richTextBox中制作快捷方式,当我希望它读取文件中的文本时。

根据此代码,文件中的文字应提取到richTextBox1

    class DragDropRichTextBox : RichTextBox
    {
    public DragDropRichTextBox()
    {
        this.AllowDrop = true;
        this.DragDrop += new DragEventHandler(DragDropRichTextBox_DragDrop);
    }

    private void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
    {
        string[] fileNames = e.Data.GetData(DataFormats.FileDrop) as string[];

        if (fileNames != null)
        {
            foreach (string name in fileNames)
            {
                try
                {
                    this.AppendText(File.ReadAllText(name) + "\n");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

关于如何使这项工作的任何想法?

2 个答案:

答案 0 :(得分:3)

在阅读文件之前,您需要检查拖动的对象。尝试以下代码。

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
            richTextBox1.AllowDrop = true;
        }

        void richTextBox1_DragDrop(object sender, DragEventArgs e)
        {
            object filename = e.Data.GetData("FileDrop");
            if (filename != null)
            {
                var list = filename as string[];

                if (list != null && !string.IsNullOrWhiteSpace(list[0]))
                {
                    richTextBox1.Clear();
                    richTextBox1.LoadFile(list[0], RichTextBoxStreamType.PlainText);
                }

            }
        }

答案 1 :(得分:2)

使用它来绑定 Designer.cs

中RichTextBox的 DragEnter DragDrop 事件
 this.richTextBox1.AllowDrop = true; this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop); this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);

private void textBox1_DragDrop(object sender, DragEventArgs e)
            {
                try
                {
                    Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
                    if (a != null)
                    {
                        string s = a.GetValue(0).ToString();
                        this.Activate();
                        OpenFile(s);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error in DragDrop function: " + ex.Message);
                }

            }

            private void OpenFile(string sFile)
            {
                try
                {
                    StreamReader StreamReader1 = new StreamReader(sFile);
                    richTextBox1.Text = StreamReader1.ReadToEnd();
                    StreamReader1.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error loading from file");
                }

            }

            private void textBox1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.None;

            }