使用OpenFileDialog组件打开多个文

Posted on 2018-12-26 18:26  努力成长静待花开  阅读(853)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  OpenFileDialog组件的Multiselect属性  //是否允许多选

  public bool Multiselect {get;ser;}

  FileNames属性    //获取所有选定的文件的文件名

   public string[] FileName {get;}

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "txt文件(*.txt)|*.txt";
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string Content = string.Empty;
                foreach(string s in openFileDialog1.FileNames)        //注意是s
                {
                    StreamReader sr = new StreamReader(s,Encoding.Default);
                    Content += sr.ReadToEnd();
                    sr.Close();
                }
                textBox1.Text = Content;
            }
        }