打开一个文件
private void button1_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { label1.Text = openFileDialog1.FileName; System.IO.StreamReader str = new System.IO.StreamReader(openFileDialog1.FileName); textBox1.Text = str.ReadToEnd(); str.Close(); } } |
打开多个文件
private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = true; if (openFileDialog1.ShowDialog()==DialogResult.OK) { foreach (var FileName in openFileDialog1.FileNames) { listView1.Items.Add(FileName); } } } |