//验证文件夹是否存在
public string DirFilePath()
{
//文件上传后的保存路径
//获取项目文件路径可参考 : https://blog.csdn.net/oshan2012/article/details/91821229
string filePath = Path.Combine(Application.StartupPath.ToString(),"ResultContentdir");
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
return filePath;
}
//方法1
private static void WriterTextContent1(string path, string content)
{
// 直接写入: File.WriteAllText(path, content);
//转换byte写入
var strByte = Encoding.UTF8.GetBytes(content);
File.WriteAllBytes(path, strByte);
}
//方法2
private static void WriterTextContent(string path, string content)
{
//if (!File.Exists(path))
// File.Create(path).Close();
using (FileStream fs = new FileStream(path, FileMode.Create))
{
var strByte = Encoding.UTF8.GetBytes(content);
fs.Write(strByte, 0, strByte.Length);
}
}
//读取text
public string ReadTextContent(string path)
{
var readText = System.IO.File.ReadAllText(path, Encoding.Default);
return readText;
}