20200126_BDDM_upperpc/SimpleServer/Tools.cs

66 lines
1.9 KiB
C#
Raw Normal View History

2023-07-27 02:53:17 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SimpleServer
{
public static class Tools
{
static Dictionary<string, bool> Txt_Used = new Dictionary<string, bool>();
#region
/// <summary>
/// 文件自动保存
/// </summary>
/// <param name="_file_name">文件名称</param>
/// <param name="path">保存路径</param>
/// <param name="logstring">文件内容</param>
public static void AddLgoToTXT(string _file_name, string path, string logstring)
{
//检查是否存在该路径
if (!System.IO.Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + _file_name;
//检查该路径下是否存在该文件
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
if (!Txt_Used.ContainsKey(path))
{
Txt_Used.Add(path, true);
}
else
{
if (Txt_Used[path])
{
return;
}
else
{
Txt_Used[path] = true;
}
}
//[1]创建文件流 文件路径 和枚举类型的文件操作类型
FileStream fs = new FileStream(path, FileMode.Append);
//[2]创建写入器
StreamWriter sw = new StreamWriter(fs);
//[3]以流的方式写入数据
sw.Write(logstring);
//[4]关闭写入器
sw.Close();
//[5]关闭文件流
fs.Close();
Txt_Used[path] = false;
}
#endregion
}
}