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 ...
随机推荐
- 25.Spark下载源码和安装和使用
安装scala 上传安装包 解压 配置scala相关的环境变量 export SCALA_HOME=/opt/modules/scala-2.11.4 export PATH=$PATH:$SCALA ...
- 控制台程序转化为windows服务
首先我们创建一个控制台应用程序WriteLog,然后添加windows服务LogService 新增的windows服务切换到代码视图,我这边用log4net进行的日志的记录 OnStart方式是服务 ...
- C/C+面试题一:找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)
已知字符串"aabbbcddddeeffffghijklmnopqrst"编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2) /********************* ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
- sublime text 2 + Dev-C++/MinGW 组合配置更方便快捷的 C/C++ 编译环境
首先看一下配置后的效果: 1.直接在底部文本框中显示运行结果(不需要从键盘输入的时候使用): 2.在cmd中运行结果(需要从键盘输入的时候使用): 快捷键说明: 运行: 在底部文本栏显示结果:Ctrl ...
- hdu 1203 转换的01包问题。。。。
俗话说的话 正难则反.,. 这个基本的思想都用不好的话 回家种田去吧. #include<cstdio> #include<string.h> #include<ios ...
- (一)weblogic11g的安装配置
一.安装 找到weblogic安装包,小编这里用的是wls1034_win32.exe版本,双击打开 完成后运行快速启动,打开快速启动界面,配置weblogic.如果没有打开,还可以在开始菜单中找到q ...
- 通过DBCC Page查看在SQL Server中哪行数据被锁住了?
原文:通过DBCC Page查看在SQL Server中哪行数据被锁住了? 如何查看被锁的是哪行数据?通过dbcc page可以. 要想明白这个问题: 首先,需要模拟阻塞问题,这里直接模拟了阻塞问题的 ...
- 验证码识别的免费 OCR
在做接口自动化以及爬虫的过程中,验证码一般是个很烦的存在,其实大厂们已经做好了一些 OCR 供使用,这里介绍一下百度 OCR 的使用方法. 注册并生成应用 1.注册一个百度智能云账号:http://a ...
- C++ STL 之 常用算法
#include <iostream> #include <vector> #include <algorithm> using namespace std; // ...