1、基于流的文本文件的读写对于C#来说比较简单,通常来讲,需要以下5个基本步骤:
- 创建一个文件流——FileStream
- 创建读取器或写入器——StreamReader/StreamWriter
- 执行读或者写操作
- 关闭读取器或写入器
- 关闭文件流
2、BinaryReader和BinaryWriter类可用于读写二进制文件
这两个类并不派生于Stream,而是直接派生于System.Object类
所有创建两个类的对象必须基于所提供的派生于Stream类的对象,如FileStream类的对象
3、File类和FileInfo类——文件基本操作
Directory类和DirectoryInfo类——目录基本操作
Path类和Enviroment类——路径类和系统信息类
4、当要读取的文件中包含汉字,需要指定编码Encoding.GetEncoding(“GB2312”)
string[ ] content=File.ReadAllLines(path,Encoding.GetEncoding(“GB2312”));
5、读写文本文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnRead_Click(object sender, EventArgs e)
{
if (ofdOpen.ShowDialog() == DialogResult.OK)
{
string filePath = ofdOpen.FileName;
txtFilePath.Text = filePath;
if (File.Exists(filePath))
{
StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312"));
string input;
while ((input = sr.ReadLine()) != null)
{
txtFileContent.Text += input + "\r\n";
}
sr.Close();
}
else
{
MessageBox.Show("您要读取的文件不存在!");
}
}
}
private void btnWrite_Click(object sender, EventArgs e)
{
sfdSave.Filter = "文本文件(*.txt)|*.txt";
if (sfdSave.ShowDialog() == DialogResult.OK)
{
string filePath = sfdSave.FileName;
StreamWriter sw = new StreamWriter(filePath, false);
sw.WriteLine(txtFileContent.Text);
sw.Close();
}
}
}
}
6、文件基本操作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic.Devices;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnCreate_Click(object sender, EventArgs e)
{
string path = @txtFile.Text;
if (File.Exists(path))
MessageBox.Show("文件名已存在");
try
{
using(StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("不想说话");
sw.WriteLine("");
sw.WriteLine("建立日期时间"+DateTime.Now.ToString());
sw.Flush();
MessageBox.Show("文件创建成功!");
}
}
catch(Exception ex)
{
MessageBox.Show("文件无法建立。" + Environment.NewLine + "请确认文件名称是否正确," + "以及您是否拥有建立权限。");
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
try
{
File.Copy(@"d:\t87.txt", @"d:\t87_1.txt", true);
FileInfo fi = new FileInfo(@"d:\t87.txt");
fi.CopyTo(@"d:\t87_2.txt", true);
MessageBox.Show("文件已复制");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void btnRename_Click(object sender, EventArgs e)
{
try
{
Computer MyComputer = new Computer();
// File.Copy(@"d:\t87.text", @"d:\t87_3.text", true);
MyComputer.FileSystem.RenameFile(@"d:\t87.txt", "更改文件名.txt");
MessageBox.Show("更改文件名成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void btnMove_Click(object sender, EventArgs e)
{
if(File.Exists(@"d:\t87.txt")){
//文件被线程占用,不能移动,解决方法可以重新Copy一个移动
File.Copy(@"d:\t87.txt", @"d:\移动.txt",true);
File.Move(@"d:\移动.txt", @"d:\移动1.txt");
MessageBox.Show("文件移动成功!");
}
else
{
FileInfo fi = new FileInfo(@"d:\t87.txt");
StreamWriter sw = fi.CreateText();
MessageBox.Show("移动前实例化文件" + fi.Name);
//文件被线程占用,不能移动
fi.CopyTo(@"d:\移动2.txt");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(@"d:\t87.txt"))
File.Delete(@"d:\t87.txt");
else
{
FileInfo fi = new FileInfo(@"d:\t87.txt");
StreamWriter sw = fi.CreateText();
MessageBox.Show("删除前实例化文件" + fi.Name);
}
MessageBox.Show("文件删除成功!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}