C# 读取、写入文件
读取文件
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# 读取、写入文件的更多相关文章
- 关于Python中读取写入文件并进行文件与用户交互的操作
一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...
- pandas-19 DataFrame读取写入文件的方法
pandas-19 DataFrame读取写入文件的方法 DataFrame有非常丰富的IO方法,比如DataFrame读写csv文件excel文件等等,操作很简单.下面在代码中标记出来一些常用的读写 ...
- c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变
c语言实现二进制(01)转化成txt格式文本: 下面的程序只能实现ascall对应字符转换,如果文件内出现中文字符,则会出现错误. 本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件 ...
- php操作文件(读取写入文件)
一,PHP如何读取文件 PHP读取文件可以读取当前服务器或远程服务器中的文件.其步骤是:打开文件.读文件和关闭文件. 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一 ...
- [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等
1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...
- java读取写入文件
先来看一下java写入文件 public static void readTxt(String value) throws IOException { FileWriter fw = null; tr ...
- Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流
/* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...
- nodejs 操作文件系统读取写入文件
我们通过fs这个模块来对文件系统进行操作,对于文件系统操作一般都有同步.异步方法,两者区别,同步等有返回结果时候,在继续执行后面的代码,异步是不等返回结果,直接执行后面的代码,待有返回结果时候,通过回 ...
- python读取/写入文件
<Python编程:从入门到实践>读书笔记 1.读取文件并且对文件内容进行打印有三种方式: with open('test.txt') as fo: for lins in fo: pri ...
- Go语言模拟文件断点续传以及多协程读取写入文件操作
今天整理了一个Go语言多协程拷贝文件的案例,之前用Java写过,这次用Go语言来写一下,代码不难,也很简单,但是网上没有案例,整了很长时间.下面贴出来,供其他人参考学习. func main(){ f ...
随机推荐
- [转帖]新一代IBM Z14主机技术介绍
新一代IBM Z14主机技术介绍 https://cloud.tencent.com/developer/news/268909 IBM最新的已经有IBM Z15 主机了.. 文章来源:企鹅号 - 云 ...
- [转帖]8个优秀Docker容器监控工具,收藏了
8个优秀Docker容器监控工具,收藏了 Docker是目前使用最广泛的容器之一,但它并不总是像物理硬件一样可见.而使用docker容器监控工具有助于消除这种透明度的缺失.以下介绍8种优秀Docker ...
- K8S从入门到放弃系列-(3)部署etcd集群
摘要:etcd 是k8s集群最重要的组件,用来存储k8s的所有服务信息, etcd 挂了,集群就挂了,我们这里把etcd部署在master三台节点上做高可用,etcd集群采用raft算法选举Leade ...
- Navicat通过跳板机连接MySQL(2层跳转)
情景描述,公司开发数据库部署在内网,而且这个开发数据库有连接需要有IP验证,就是只能在内网的某个IP才能连接,所以每次连接都会先连接外网能访问的跳板机,在从跳板机上ssh到内网上的A机器,在从A机 ...
- C++:盾神与条状项链
实现代码如下: #include<cstdlib> #include <string> #include <iostream> using namespace st ...
- 封装ADO库之MFC应用
Microsoft Activex Data Objects(ADO)支持用于建立基于客户端/服务器和web的应用程序开发的主要功能.其主要优点是易于使用.高速度.低内存支出和占用磁盘空间较少. 本次 ...
- 验证码处理+cookie模拟登录
一.背景 相关博文:https://www.jianshu.com/p/9fce799edf1e https://blog.csdn.net/h19910518/article/details/793 ...
- WUSTOJ 1232: 矩阵乘法(C)
1232: 矩阵乘法 Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld Description 小明明正在学习线性代数,老师布置 ...
- 怎样手写实现 new 命令的功能
实现new命令的功能, 首先需要知道new命令都做了些什么: 第一步: 创建一个空对象, 作为实例对象的容器; 第二步: 将空对象的prototype指向构造函数的prototype; 第三步: 将空 ...
- [JZOJ5465]道路重建--边双缩点+树的直径
题目链接 lueluelue 分析 这鬼题卡了我10发提交,之前做过一道类似的题目:https://rye-catcher.github.io/2018/07/09/luogu%E9%A2%98%E8 ...