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控件

上一篇:SVG地图 经纬度 转换 相关文章及知识点


下一篇:JS组件系列——Bootstrap文件上传组件:bootstrap fileinput