加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#编程-120:文件选择之OpenFileDialog控件

(2017-08-14 22:05:01)
标签:

csharp

分类: 科技
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace OpenFileDialogTest
  12. {
  13.     public partial class Form1 Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void btnOpen_Click(object sender, EventArgs e)
  21.         {
  22.             OpenFileDialog ofd new OpenFileDialog();
  23.             ofd.InitialDirectory @"C:\Users\pengshiyu\Desktop\destination";
  24.             //设置文件筛选器
  25.             ofd.Filter "文本文件(*.txt)|*.txt|Word文件(*.doc,*.docx)|*.doc;*.docx|所有文件(*.*)|*.*";
  26.             ofd.FilterIndex 1;
  27.             ofd.Title "openFileDialog实例";
  28.             ofd.FileName "123";
  29.             ofd.ShowHelp true;
  30.             if (ofd.ShowDialog() == DialogResult.OK)
  31.             {
  32.                 string fName ofd.FileName;
  33.                 string fileCon "";
  34.                 StreamReader sr new StreamReader(fName, Encoding.GetEncoding("gb2312"));
  35.                 while ((fileCon sr.ReadLine()) != null)
  36.                 {
  37.                     txtShow.Text += fileCon;
  38.                 }
  39.                 sr.Close();
  40.  
  41.             }
  42.         }
  43.     }
  44. }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有