c#读写txt文档代码
来源:互联网 发布:沈阳搜索引擎排名数据 编辑:程序博客网 时间:2024/05/29 02:14
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;
}
}
}
}
}
- c#读写txt文档代码
- C# 读写txt文档
- 读写txt文档
- txt文档读写注意事项
- c++读写txt文档
- C#读写txt文件
- c#读写txt文件
- c#读写txt文件
- C#读写TXT文件
- C#读写txt文件
- C#读写txt
- C# 读写txt
- C# txt文件读写
- C#读写txt文件
- C#读写txt文件
- C#文本文件(.txt)读写
- java读写Txt文档(转)
- Java读写txt文档内容
- C++ Note1
- 数据库的备份与还原
- 新设想的进展
- .net中语音识别和语音合成
- Using Valgrind to Find Memory Leaks
- c#读写txt文档代码
- 小小问题集锦13之---关于分组加顺序号的SQL写法
- cms文章管理
- CCPHONE的特点
- 判断一个字符串是否为整数
- 小小问题集锦12之---查找具有IDENTITY列的表和其列
- Oracle数据库、实例、ORACLE_SID、数据库域名的区别
- 到目前为止的进展情况(v.1.0.0.4)
- 一些数据库的操作(创建,修改,set,收缩,扩大,分离附加等