读取文件

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

Form

 namespace ReadWriteText
{
public partial class Form1 : Form
{
private readonly OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();
private string chosenFile; public Form1()
{
InitializeComponent(); menuFileOpen.Click += OnFileOpen;
chooseOpenFileDialog.FileOk += OnOpenFileDialogOK; menuFileSave.Click += OnFileSave;
} private void OnFileOpen(object Sender, EventArgs e)
{
chooseOpenFileDialog.ShowDialog();
} private void OnFileSave(object Sender, EventArgs e)
{
SaveFile();
} private void OnOpenFileDialogOK(object Sender, CancelEventArgs e)
{
chosenFile = chooseOpenFileDialog.FileName;
Text = Path.GetFileName(chosenFile);
DisplayFile();
} private void SaveFile()
{
StreamWriter sw = new StreamWriter(chosenFile, false,
Encoding.Unicode);
foreach (string line in textBoxContents.Lines)
sw.WriteLine(line);
sw.Close();
} private void DisplayFile()
{
StringCollection linesCollection = ReadFileIntoStringCollection();
string[] linesArray = new string[linesCollection.Count];
linesCollection.CopyTo(linesArray, );
textBoxContents.Lines = linesArray;
} private StringCollection ReadFileIntoStringCollection()
{
const int MaxBytes = ;
StreamReader sr = new StreamReader(chosenFile);
StringCollection result = new StringCollection();
int nBytesRead = ;
string nextLine;
while ((nextLine = sr.ReadLine()) != null)
{
nBytesRead += nextLine.Length;
if (nBytesRead > MaxBytes)
break;
result.Add(nextLine);
}
sr.Close();
return result;
}
}
}

Designer

 namespace ReadWriteText
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.menuFileOpen = new System.Windows.Forms.MenuItem();
this.textBoxContents = new System.Windows.Forms.TextBox();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuFileSave = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.SuspendLayout();
//
// menuFileOpen
//
this.menuFileOpen.Index = ;
this.menuFileOpen.Text = "&Open";
//
// textBoxContents
//
this.textBoxContents.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxContents.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBoxContents.Location = new System.Drawing.Point(, );
this.textBoxContents.Multiline = true;
this.textBoxContents.Name = "textBoxContents";
this.textBoxContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBoxContents.Size = new System.Drawing.Size(, );
this.textBoxContents.TabIndex = ;
//
// menuItem1
//
this.menuItem1.Index = ;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFileOpen,
this.menuFileSave});
this.menuItem1.Text = "&File";
//
// menuFileSave
//
this.menuFileSave.Index = ;
this.menuFileSave.Text = "&Save";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.textBoxContents);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.MenuItem menuFileOpen;
private System.Windows.Forms.TextBox textBoxContents;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuFileSave;
private System.Windows.Forms.MainMenu mainMenu1;
}
}

最新文章

  1. AndRodi Strudio中的按钮时件
  2. OpenCV配置经历简述
  3. getWinSystemIcon
  4. 二级c程序设计题(2)
  5. java无符号移位(&gt;&gt;&gt;)和有符号移位(&gt;&gt;)
  6. 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
  7. win7下Java环境变量配置及说明
  8. (简单) POJ 2502 Subway,Dijkstra。
  9. Ubuntu离线安装Sogou拼音(附老版本安装&amp;输入法自启动)
  10. Example011表单中修改内容
  11. Linux CentOS安装配置MySQL数据库
  12. avue的小白之路
  13. CMDB服务器管理系统【s5day88】:采集资产-文件配置(二)
  14. linux----------CentOS的一些除了yum安装以外的基本操作命令。
  15. Atom使用
  16. asp.net 后台 get,post请求
  17. SpringMVC MultiActionController 默认方法名解析器
  18. I.MX6 使用Ubuntu文件系统
  19. 简单webpack plugin 开发
  20. 【问题解决】docker中创建volume时,无访问权限

热门文章

  1. 【转帖】一文看懂docker容器技术架构及其中的各个模块
  2. HTTP最常见的请求头
  3. Office 2016、2019 与 Office 365 的区别
  4. XDebug调试
  5. SAS学习笔记41 宏变量存储及间接引用
  6. java多线程:继承Thread和实现Runable接口的区别
  7. C#特性 详解
  8. javascript的装载和执行
  9. Trie树(字典树)-题解 P2580 【于是他错误的点名开始了】
  10. Go part 4 数据容器(数组,slice,string,map,syncMap,list)