码迷,mamicode.com
首页 > Windows程序 > 详细

C#文件流读写文件的简单winform实现

时间:2015-12-04 18:11:33      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace BKY
13 {
14     public partial class MainFrm : Form
15     {
16         public MainFrm()
17         {
18             InitializeComponent();
19         }
20 
21         private void btnOpen_Click(object sender, EventArgs e)
22         {
23             using (OpenFileDialog ofd = new OpenFileDialog())
24             {
25                 if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
26                 {
27                     return;
28                 }
29 
30             using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
31                 {
32                     using (StreamReader sr = new StreamReader(fs))
33                     {
34 
35                             byte[] data = new byte[1024 * 1024 * 5];
36                             int r = fs.Read(data, 0, data.Length);
37                             string line = Encoding.UTF8.GetString(data, 0, data.Length);
38                             this.txtContent.Text += line;            
39                             fs.Close();
40 
41                             fs.Dispose();
42                         
43                     }
44                 }
45             }
46         }
47 
48         private void btnSave_Click(object sender, EventArgs e)
49         {
50             using (SaveFileDialog sfd = new SaveFileDialog())
51             {
52                 if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
53                 {
54                     return;
55                 }
56                 using (FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
57                 {
58                     string txt = txtContent.Text.Replace("\n","\r\n");
59                     byte[] data = Encoding.UTF8.GetBytes(txt);
60                     fs.Write(data, 0, data.Length);
61                 }
62             }
63         }
64     }
65 }

 

C#文件流读写文件的简单winform实现

标签:

原文地址:http://www.cnblogs.com/laresh/p/5019672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!