天天看點

C#程式設計-120:檔案選擇之OpenFileDialog控件

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();
 
            }
        }
    }
}      
C#程式設計-120:檔案選擇之OpenFileDialog控件
C#程式設計-120:檔案選擇之OpenFileDialog控件