C#中文件流的操作
刚刚学习了C#中文件流的相关操作,自己练习了一下,做了一个可以实现输入/输出文件中内容的简单例子。
由于水平有限,有许多需要改进的地方。
效果如下:
单击“write”按钮
添加文件
想文件中添加内容
单击“read”
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace IO
{
publicpartialclass Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text ="IO/read&write";
}
privatevoid RedBtn_Click(object sender, EventArgs e)
{
string content;
string path = openFileDialog1.FileName.ToString();
if (!File.Exists(path))
{
MessageBox.Show("Can not find the file !");
}
try
{
if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
{
////创建文件流
FileStream fs =new FileStream(path, FileMode.Open, FileAccess.Read);
////创建读取器
StreamReader fr =new StreamReader(fs);
////读取文件所有内容
content = fr.ReadToEnd();
txtContent.Text = content;
////关闭读取器
fr.Close();
////关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
privatevoid WrtBtn_Click(object sender, EventArgs e)
{
string Content = txtContent.Text;
string path = openFileDialog1.FileName.ToString();
if (!File.Exists(path))
{
MessageBox.Show("Can not find the file !");
}
try
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
////创建文件流
FileStream fs =new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
////创建写入器
StreamWriter fw =new StreamWriter(fs);
////将录入的内容写入文件
fw.Write(Content);
////关闭读取器
fw.Close();
////关闭文件流
fs.Close();
MessageBox.Show("写入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace IO
{
publicpartialclass Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text ="IO/read&write";
}
privatevoid RedBtn_Click(object sender, EventArgs e)
{
string content;
string path = openFileDialog1.FileName.ToString();
if (!File.Exists(path))
{
MessageBox.Show("Can not find the file !");
}
try
{
if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
{
////创建文件流
FileStream fs =new FileStream(path, FileMode.Open, FileAccess.Read);
////创建读取器
StreamReader fr =new StreamReader(fs);
////读取文件所有内容
content = fr.ReadToEnd();
txtContent.Text = content;
////关闭读取器
fr.Close();
////关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
privatevoid WrtBtn_Click(object sender, EventArgs e)
{
string Content = txtContent.Text;
string path = openFileDialog1.FileName.ToString();
if (!File.Exists(path))
{
MessageBox.Show("Can not find the file !");
}
try
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
////创建文件流
FileStream fs =new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
////创建写入器
StreamWriter fw =new StreamWriter(fs);
////将录入的内容写入文件
fw.Write(Content);
////关闭读取器
fw.Close();
////关闭文件流
fs.Close();
MessageBox.Show("写入成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}