using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace OpenFileDialogTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = @"C:\Users\pengshiyu\Desktop\destination"; //设置文件筛选器 ofd.Filter = "文本文件(*.txt)|*.txt|Word文件(*.doc,*.docx)|*.doc;*.docx|所有文件(*.*)|*.*"; ofd.FilterIndex = 1; ofd.Title = "openFileDialog实例"; ofd.FileName = "123"; ofd.ShowHelp = true; if (ofd.ShowDialog() == DialogResult.OK) { string fName = ofd.FileName; string fileCon = ""; StreamReader sr = new StreamReader(fName, Encoding.GetEncoding("gb2312")); while ((fileCon = sr.ReadLine()) != null) { txtShow.Text += fileCon; } sr.Close(); } } } }