c#读写txt文档代码
2010-07-20 13:10
211 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace ReadWriteTxt
{
public partial class Form1 : Form
{
string fileName;
System.IO.StreamReader st;//文件读取器
int num_of_line =-1;//读取的行数
string wholeTxt;//存放整体文件
string TxtToWrite;//存放要写入的数据
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
////////////打开文本
DataSet myDs = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/
openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.ShowDialog();
////////////获取文件名
fileName = openFileDialog.FileName;
///////////读取文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码
wholeTxt = st.ReadToEnd();
st.Close();//关闭读入器
this.textBox1.Text = wholeTxt;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
num_of_line=num_of_line+1;
if (num_of_line == 0)
{
////////////打开文本
DataSet myDs = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/
openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.ShowDialog();
////////////获取文件名
fileName = openFileDialog.FileName;
///////////读取文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码
st = File.OpenText(fileName);
//wholeTxt = st.ReadToEnd();
//st.Close();//关闭读入器
//this.lineTxt = wholeTxt.Split('/r');//按行分割
//textBox1.Text = lineTxt[num_of_line]+"/r/n";/////注释掉的这部分内容是另一种读取方法:先整体读取,然后按行分割读取房贷lineTxt[]中
textBox1.Text = st.ReadLine();
}
}
else
{
if (st.ReadLine() != null)
{
textBox1.Text = textBox1.Text + st.ReadLine();
}
else
{
MessageBox.Show("已经读取到最后一行!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
TxtToWrite = textBox1.Text;
if (TxtToWrite == string.Empty)
{
MessageBox.Show("TextBox 内容不能为空!");
}
else
{
SaveFileDialog savefiledialog = new SaveFileDialog();
savefiledialog.Filter = "文本文件(*.txt)|*.txt";
if (savefiledialog.ShowDialog() == DialogResult.OK)
{
//使用“另存为”对话框中输入的文件名实例化StreamWriter对象
StreamWriter sw = new StreamWriter(savefiledialog.FileName, true);
//向创建的文件中写入内容
sw.WriteLine(textBox1.Text);
//关闭当前文件写入流
sw.Close();
textBox1.Text = string.Empty;
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace ReadWriteTxt
{
public partial class Form1 : Form
{
string fileName;
System.IO.StreamReader st;//文件读取器
int num_of_line =-1;//读取的行数
string wholeTxt;//存放整体文件
string TxtToWrite;//存放要写入的数据
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
////////////打开文本
DataSet myDs = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/
openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.ShowDialog();
////////////获取文件名
fileName = openFileDialog.FileName;
///////////读取文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码
wholeTxt = st.ReadToEnd();
st.Close();//关闭读入器
this.textBox1.Text = wholeTxt;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
num_of_line=num_of_line+1;
if (num_of_line == 0)
{
////////////打开文本
DataSet myDs = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";//注意这里写路径时要用c://而不是c:/
openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
openFileDialog.ShowDialog();
////////////获取文件名
fileName = openFileDialog.FileName;
///////////读取文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//st = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);//UTF8为编码
st = File.OpenText(fileName);
//wholeTxt = st.ReadToEnd();
//st.Close();//关闭读入器
//this.lineTxt = wholeTxt.Split('/r');//按行分割
//textBox1.Text = lineTxt[num_of_line]+"/r/n";/////注释掉的这部分内容是另一种读取方法:先整体读取,然后按行分割读取房贷lineTxt[]中
textBox1.Text = st.ReadLine();
}
}
else
{
if (st.ReadLine() != null)
{
textBox1.Text = textBox1.Text + st.ReadLine();
}
else
{
MessageBox.Show("已经读取到最后一行!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
TxtToWrite = textBox1.Text;
if (TxtToWrite == string.Empty)
{
MessageBox.Show("TextBox 内容不能为空!");
}
else
{
SaveFileDialog savefiledialog = new SaveFileDialog();
savefiledialog.Filter = "文本文件(*.txt)|*.txt";
if (savefiledialog.ShowDialog() == DialogResult.OK)
{
//使用“另存为”对话框中输入的文件名实例化StreamWriter对象
StreamWriter sw = new StreamWriter(savefiledialog.FileName, true);
//向创建的文件中写入内容
sw.WriteLine(textBox1.Text);
//关闭当前文件写入流
sw.Close();
textBox1.Text = string.Empty;
}
}
}
}
}
相关文章推荐
- C# 读写txt文档
- C#读写txt文件多种方法实例代码
- 『C#基础』C#读写TXT文档
- C#:TXT文件读写(VS2010窗体+代码)
- c#读写txt文本文档
- C#读写txt文件的两种方法介绍
- C#读写mysql的blob数据代码样例
- 【.Net】C#文本文件(.txt)读写
- c# 读写txt
- 在C#里面给PPT文档添加注释的实现代码
- C# 把string字符导出到txt文档方法
- asp.net读写添加修改删除操作xml文件 代码如下(C#)
- C#读写txt文件的两种方法介绍
- Unity下关于C#的文件读写一(基于TXT的简单读写)
- C#读写txt文件数据的两种方法
- MFC读写固定格式的txt文档+生成目录等
- C# 代码结构自动化生成文档工具开发
- SharePoint下用C#代码上传文档至文档库的子文件夹中
- 解决C#中txt文档导入数据库时,中文显示乱码的问题
- C# txt文件读写