//写入文件
public void WriteFile(string Content, string FileSavePath)
{
if(System.IO.File.Exists(FileSavePath))
{
System.IO.File.Delete(FileSavePath);
}
System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
Byte[] bContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Content);
fs.Write(bContent, 0,bContent.Length);
fs.Close();
fs = null;
}
//读取文件
public string ReadFile(string FilePath)
{
System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
string StrRead = rd.ReadToEnd().ToString();
rd.Close();
return StrRead;
}
用到以下命名空间
Imports System
Imports System.Data
Imports System.IO
Imports System.Text
public void WriteFile(string Content, string FileSavePath)
{
if(System.IO.File.Exists(FileSavePath))
{
System.IO.File.Delete(FileSavePath);
}
System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
Byte[] bContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Content);
fs.Write(bContent, 0,bContent.Length);
fs.Close();
fs = null;
}
//读取文件
public string ReadFile(string FilePath)
{
System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
string StrRead = rd.ReadToEnd().ToString();
rd.Close();
return StrRead;
}
用到以下命名空间
Imports System
Imports System.Data
Imports System.IO
Imports System.Text

本文介绍了使用C#进行文件的读写操作方法,包括如何创建文件、写入内容及读取文件的具体实现。文中提供了具体的代码示例,展示了如何通过FileStream和File类来完成这些任务。
1288

被折叠的 条评论
为什么被折叠?



