读取文件

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;
}
}

C# 读取、写入文件的更多相关文章

  1. 关于Python中读取写入文件并进行文件与用户交互的操作

    一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...

  2. pandas-19 DataFrame读取写入文件的方法

    pandas-19 DataFrame读取写入文件的方法 DataFrame有非常丰富的IO方法,比如DataFrame读写csv文件excel文件等等,操作很简单.下面在代码中标记出来一些常用的读写 ...

  3. c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变

    c语言实现二进制(01)转化成txt格式文本: 下面的程序只能实现ascall对应字符转换,如果文件内出现中文字符,则会出现错误. 本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件 ...

  4. php操作文件(读取写入文件)

    一,PHP如何读取文件 PHP读取文件可以读取当前服务器或远程服务器中的文件.其步骤是:打开文件.读文件和关闭文件. 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一 ...

  5. [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等

    1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...

  6. java读取写入文件

    先来看一下java写入文件 public static void readTxt(String value) throws IOException { FileWriter fw = null; tr ...

  7. Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流

    /* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...

  8. nodejs 操作文件系统读取写入文件

    我们通过fs这个模块来对文件系统进行操作,对于文件系统操作一般都有同步.异步方法,两者区别,同步等有返回结果时候,在继续执行后面的代码,异步是不等返回结果,直接执行后面的代码,待有返回结果时候,通过回 ...

  9. python读取/写入文件

    <Python编程:从入门到实践>读书笔记 1.读取文件并且对文件内容进行打印有三种方式: with open('test.txt') as fo: for lins in fo: pri ...

  10. Go语言模拟文件断点续传以及多协程读取写入文件操作

    今天整理了一个Go语言多协程拷贝文件的案例,之前用Java写过,这次用Go语言来写一下,代码不难,也很简单,但是网上没有案例,整了很长时间.下面贴出来,供其他人参考学习. func main(){ f ...

随机推荐

  1. java中整数的默认为int类型的一些问题

    thingking in java 读书感悟 作者 :淮左白衣 写于2018年4月8日17:51:44 关于整数的默认类型,以及会产生的一些小问题 涉及基本数据类型的重载 关于整数的默认类型,以及会产 ...

  2. python学习-17 列表list 2

    # 1. 选择嵌套列表里的元素(内部进行了for循环) li = [1,2,",45]],"abc",True] a = li[3][2][1] print(a) 运行结 ...

  3. Windows下命令行Git无法显示中文问题解决方案

    Windows下Git设置编码正常显示中文: 在 CMD 下设置环境变量 set LESSCHARSET=utf-8 在 PowerShell 下设置环境变量 $env:LESSCHARSET='ut ...

  4. 基于Task的多线程

    /// <summary> /// 基于Task的多线程 /// </summary> public class Tasks { public static void Task ...

  5. 【解决方法】You seem to have the current working directory in your LD_LIBRARY_PATH environment variable.

    参考地址:https://blog.csdn.net/qq_24755999/article/details/78722788 You seem to have the current working ...

  6. CentOS 7.X 静默安装Oracle 12C数据库

    环境 System : CentOS 7.x jrxxfwb-zrgldb://> uname -a Linux jrxxfwb-zrgldb 3.10.0-693.17.1.el7.x86_6 ...

  7. NHiberante从.net framework转移到.net standard(.net core 2.2)时遇到的坑及填坑

    在.net framework中的创建session代码先贴一个 public class SessionBuilder { private static ISessionFactory _sessi ...

  8. 【es6】es6使用集锦

    一.查找数组中是否包含某个元素  使用includes  ,返回值为布尔值 arr.includes(searchElement, fromIndex): 解析:searchElement   查询元 ...

  9. 基于【 centos7】五 || GitLab环境搭建

    一.基于Docker部署GitLab环境搭建 1.下载镜像 docker pull beginor/gitlab-ce:11.0.1-ce.0 2.创建GitLab 的配置 (etc) . 日志 (l ...

  10. vue、react中循环遍历为什么会有key,key有什么作用?

    先讲一下,vue和react都是在操作虚拟dom,并且根据diff算法进行新旧dom对比,从而更新dom,以vue举例: vue官方文档中写到有 key 的特殊属性主要用在 Vue 的虚拟 DOM 算 ...