C# 利用 FileInfo 读写文件
/// <summary>
/// 获取指定文本文件中的内容
/// </summary>
/// <param name="paht">文件路径</param>
/// <returns>返回文本内容</returns>
private string GetText(string paht)
{
string result = string.Empty;
try
{
FileInfo file = new FileInfo(paht);
result = file.OpenText().ReadToEnd();
return result;
}
catch (Exception ex)
{
return result;
}
}
/// <summary>
/// 本地创建文件并写入
/// </summary>
/// <param name="path">写入路径</param>
/// <param name="text">写入内容</param>
/// <returns></returns>
private bool SetText(string path, string text)
{
try
{
FileInfo file = new FileInfo(path);
// Delete the file if it exists.
if (file.Exists)
{
file.Delete();
}
//Create the file.
using (FileStream fs = file.Create())
{
Byte[] info = new UTF8Encoding(true).GetBytes(text);
//Add some information to the file.
fs.Write(info, 0, info.Length);
}
return true;
}
catch (Exception ex)
{
return false;
}
}