文件读写:需要实现的功能是:点击1,弹出文件打开对话框OpenFiledialog,选择要读取的文件,点击确定之后,把文件的路径显示在1上,然后点击读取,把文件的内容显示在3上面;
同理,文件的写入,我们在3中写好内容之后,点击2,弹出文件保存对话框SaveFiledialog,然后选择好路径之后,点击写入,就将我们在3中写好的内容,保存在指定的路径了。
现在实现功能:
PRivatevoidtxtReadPath_Click(objectsender, EventArgs e)
{//打开文件对话框using(OpenFileDialog openFileDialog =newOpenFileDialog())
{if(openFileDialog.ShowDialog() ==DialogResult.OK)
{//显示文件的全路径txtReadPath.Text =openFileDialog.FileName;
}
}
}///<summary>///读取文件///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidbtnNewRead_Click(objectsender, EventArgs e)
{//创建文件流(读文件的时候,FileMode选择Open)using(FileStream fileRead =newFileStream(txtReadPath.Text,FileMode.Open))
{//创建btpe数组byte[] fileByte=newbyte[fileRead.Length];//把文件数据读取到数组中fileRead.Read(fileByte,0, fileByte.Length);//现在将数据转化为string格式的数据,好显示在txt控件中,通过下面的方法把byte数组转化为string字符串txtContent.Text=Encoding.Default.GetString(fileByte);}
}
这部分就实现了读取文件的数据到文本框中,其中Encoding.Default.GetString这个方法只能,读取文本文件。不能读取Word,所谓文本文件就是可以用记事本打开的。
Encoding.Default.GetString这个方法还有个问题,会乱码:
解决方法:
txtContent.Text = Encoding.GetEncoding("utf-8").GetString(fileByte);
修正之后:
好了,下面开始文件的写入功能实现:
步骤:我们先在文本框中输入内容,然后点击第二个文本框,弹出保存对话框,我们选择路径,输入要保存的名字(带上后缀)。然后点击保存。
所有的代码如下:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceFileReadDemo
{publicpartialclassForm1 : Form
{publicForm1()
{
InitializeComponent();
}privatevoidtxtReadPath_Click(objectsender, EventArgs e)
{//打开文件对话框using(OpenFileDialog openFileDialog =newOpenFileDialog())
{if(openFileDialog.ShowDialog() ==DialogResult.OK)
{//显示文件的全路径txtReadPath.Text =openFileDialog.FileName;
}
}
}///<summary>///读取文件///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidbtnNewRead_Click(objectsender, EventArgs e)
{//创建文件流(读文件的时候,FileMode选择Open)using(FileStream fileRead =newFileStream(txtReadPath.Text,FileMode.Open))
{//创建btpe数组byte[] fileByte=newbyte[fileRead.Length];//把文件数据读取到数组中fileRead.Read(fileByte,0, fileByte.Length);//现在将数据转化为string格式的数据,好显示在txt控件中,通过下面的方法把byte数组转化为string字符串//txtContent.Text= Encoding.Default.GetString(fileByte);//这个方法会乱码、txtContent.Text = Encoding.GetEncoding("utf-8").GetString(fileByte);
}
}///<summary>///文件写入///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidtextWritePath_Click(objectsender, EventArgs e)
{//创建文件保存对话框using(SaveFileDialog saveFileDialog =newSaveFileDialog())
{if(saveFileDialog.ShowDialog() ==DialogResult.OK)
{//全路径textWritePath. Text =saveFileDialog.FileName;
}
}
}privatevoidbtnWrite_Click(objectsender, EventArgs e)
{//将内容写入到指定的路径文件中using(FileStream fileWrite =newFileStream(textWritePath.Text.Trim(),FileMode.Create))
{//Write方法,需要传入Byte数组,这个时候,就有个问题了,我们在文本控件中,输入的内容是字符串的,不过同理,字串串也可以转化为byte数组byte[] fileWriteByte=Encoding.GetEncoding("utf-8").GetBytes(txtContent.Text.Trim());
fileWrite.Write(fileWriteByte,0, fileWriteByte.Length);
}
}
}
}