1. 程式人生 > >使用OpenFileDialog元件開啟多個文

使用OpenFileDialog元件開啟多個文

實現效果:

  

知識運用:

  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;
            }
        }