C# 读取写入文件

Posted 指间的徘徊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 读取写入文件相关的知识,希望对你有一定的参考价值。

读取文件

File.ReadAllText(textBox1.Text,Encoding.ASCII);

Form

 1 namespace ReadWriteText
 2 {
 3     public partial class Form1 : Form
 4     {
 5         private readonly OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();
 6         private string chosenFile;
 7 
 8         public Form1()
 9         {
10             InitializeComponent();
11 
12             menuFileOpen.Click += OnFileOpen;
13             chooseOpenFileDialog.FileOk += OnOpenFileDialogOK;
14 
15             menuFileSave.Click += OnFileSave;
16         }
17 
18         private void OnFileOpen(object Sender, EventArgs e)
19         {
20             chooseOpenFileDialog.ShowDialog();
21         }
22 
23         private void OnFileSave(object Sender, EventArgs e)
24         {
25             SaveFile();
26         }
27 
28         private void OnOpenFileDialogOK(object Sender, CancelEventArgs e)
29         {
30             chosenFile = chooseOpenFileDialog.FileName;
31             Text = Path.GetFileName(chosenFile);
32             DisplayFile();
33         }
34 
35         private void SaveFile()
36         {
37             StreamWriter sw = new StreamWriter(chosenFile, false,
38                                                Encoding.Unicode);
39             foreach (string line in textBoxContents.Lines)
40                 sw.WriteLine(line);
41             sw.Close();
42         }
43 
44         private void DisplayFile()
45         {
46             StringCollection linesCollection = ReadFileIntoStringCollection();
47             string[] linesArray = new string[linesCollection.Count];
48             linesCollection.CopyTo(linesArray, 0);
49             textBoxContents.Lines = linesArray;
50         }
51 
52         private StringCollection ReadFileIntoStringCollection()
53         {
54             const int MaxBytes = 65536;
55             StreamReader sr = new StreamReader(chosenFile);
56             StringCollection result = new StringCollection();
57             int nBytesRead = 0;
58             string nextLine;
59             while ((nextLine = sr.ReadLine()) != null)
60             {
61                 nBytesRead += nextLine.Length;
62                 if (nBytesRead > MaxBytes)
63                     break;
64                 result.Add(nextLine);
65             }
66             sr.Close();
67             return result;
68         }
69     }
70 }
View Code

Designer

 1 namespace ReadWriteText
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows Form Designer generated code
24 
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.components = new System.ComponentModel.Container();
32             this.menuFileOpen = new System.Windows.Forms.MenuItem();
33             this.textBoxContents = new System.Windows.Forms.TextBox();
34             this.menuItem1 = new System.Windows.Forms.MenuItem();
35             this.menuFileSave = new System.Windows.Forms.MenuItem();
36             this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
37             this.SuspendLayout();
38             // 
39             // menuFileOpen
40             // 
41             this.menuFileOpen.Index = 0;
42             this.menuFileOpen.Text = "&Open";
43             // 
44             // textBoxContents
45             // 
46             this.textBoxContents.Dock = System.Windows.Forms.DockStyle.Fill;
47             this.textBoxContents.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
48             this.textBoxContents.Location = new System.Drawing.Point(0, 0);
49             this.textBoxContents.Multiline = true;
50             this.textBoxContents.Name = "textBoxContents";
51             this.textBoxContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
52             this.textBoxContents.Size = new System.Drawing.Size(284, 245);
53             this.textBoxContents.TabIndex = 1;
54             // 
55             // menuItem1
56             // 
57             this.menuItem1.Index = 0;
58             this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
59             this.menuFileOpen,
60             this.menuFileSave});
61             this.menuItem1.Text = "&File";
62             // 
63             // menuFileSave
64             // 
65             this.menuFileSave.Index = 1;
66             this.menuFileSave.Text = "&Save";
67             // 
68             // mainMenu1
69             // 
70             this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
71             this.menuItem1});
72             // 
73             // Form1
74             // 
75             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
76             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
77             this.ClientSize = new System.Drawing.Size(284, 245);
78             this.Controls.Add(this.textBoxContents);
79             this.Menu = this.mainMenu1;
80             this.Name = "Form1";
81             this.Text = "Form1";
82             this.ResumeLayout(false);
83             this.PerformLayout();
84 
85         }
86 
87         #endregion
88 
89         private System.Windows.Forms.MenuItem menuFileOpen;
90         private System.Windows.Forms.TextBox textBoxContents;
91         private System.Windows.Forms.MenuItem menuItem1;
92         private System.Windows.Forms.MenuItem menuFileSave;
93         private System.Windows.Forms.MainMenu mainMenu1;
94     }
95 }
View Code

 

以上是关于C# 读取写入文件的主要内容,如果未能解决你的问题,请参考以下文章

写入/读取文本文件 (C#)

C#文件读取,FileSteam和BinaryWriter/Reader的使用

C# .NET 工具类 一Excel 读取与写入

C# 读取写入文件

C#读取Excle文件时报错,怎么处理?

文件的读取和写入