//新增txt文件内容
public static string InsertSql(string tabName, string sql)
{
try
{
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/") + "SQLTxt";
string title = "\\" + tabName + ".txt";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
//没有则创建这个文件
using (FileStream fs = new FileStream(path + title, FileMode.Create, FileAccess.Write))
{
byte[] buffer = Encoding.UTF8.GetBytes(sql);
fs.Write(buffer, 0, buffer.Length);
}
return path + title;
}
else
{
//没有则创建这个文件
using (FileStream fs = new FileStream(path + title, FileMode.Create, FileAccess.Write))
{
byte[] buffer = Encoding.UTF8.GetBytes(sql);
fs.Write(buffer, 0, buffer.Length);
}
return path + title;
}
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 获取txt文件内容
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetContentByTxt(string path)
{
string conStr = "";
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader(fs);
conStr = reader.ReadToEnd();
}
catch (Exception)
{
conStr = "";
}
return conStr;
}
/// <summary>
/// 删除txt文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static void DeleteTxt(string fileFullPath)
{
try
{
if (File.Exists(fileFullPath))
{
// 2、根据路径字符串判断是文件还是文件夹
FileAttributes attr = File.GetAttributes(fileFullPath);
// 3、根据具体类型进行删除
if (attr == FileAttributes.Directory)
{
// 3.1、删除文件夹
Directory.Delete(fileFullPath, true);
}
else
{
// 3.2、删除文件
File.Delete(fileFullPath);
}
File.Delete(fileFullPath);
}
}
catch (Exception e)
{}
}