private void 跟踪记录(string 输出内容)
{
String 保存路径 = @System.Environment.CurrentDirectory + "\\过程跟踪";
if (!Directory.Exists(保存路径))
Directory.CreateDirectory(保存路径);
String 保存文件名 = @保存路径 + "\\过程跟踪记录.txt ";
StreamWriter 写入 = new StreamWriter(保存文件名, true);//以可以追加文本的方式打开文件流
写入.WriteLine(输出内容);
写入.Flush();
写入.Close();
System.Diagnostics.Process.Start("notepad.exe", 保存文件名);
}
private void 修改日记(string 输出内容)
{
String 保存路径 = @System.Environment.CurrentDirectory + "\\修改日记";
if (Directory.Exists(保存路径))
{
String 保存文件名 = @保存路径 + "\\" + 时间.ToLongDateString() + ".txt ";
FileInfo 文件 = new FileInfo(保存文件名);
if (!文件.Exists) { FileStream 创建或覆盖 = File.Create(保存文件名); 创建或覆盖.Flush(); 创建或覆盖.Close(); }
//{ 文件.Create(); }FileStream 创建只写文件 = 文件.OpenWrite(); 创建只写文件.Close();
//StreamWriter 写入 = File.AppendText(保存文件名);//以可以追加文本的方式打开文件流
StreamWriter 写入 = new StreamWriter(保存文件名, true);//以可以追加文本的方式打开文件流
写入.WriteLine(输出内容); 写入.Flush(); 写入.Close();
}
else
{
Directory.CreateDirectory(保存路径);
String 保存文件名 = @保存路径 + "\\" + 时间.ToLongDateString() + ".txt ";
FileStream 创建或覆盖 = File.Create(保存文件名);
创建或覆盖.Flush(); 创建或覆盖.Close();
StreamWriter 写入 = new StreamWriter(保存文件名);//不可追加文本
写入.WriteLine(输出内容); 写入.Flush(); 写入.Close();
}
}
编码问题修改代码如下:参考资料
static void 写改记录(string 输出内容, string 文件夹, string 文件名, string 文件序, bool 选择 = true)
{
String 保存路径 = @System.Environment.CurrentDirectory + "\\" + 文件夹;
string 创建文件 = @保存路径 + "\\" + 文件序 + ".txt ";
try
{
Thread 写入文本 = new Thread(delegate()
{
if (!Directory.Exists(保存路径))
Directory.CreateDirectory(保存路径);
String 保存文件名 = @保存路径 + "\\" + 文件名 + ".txt ";
FileInfo 文件 = new FileInfo(保存文件名);
StreamWriter 写入 = new StreamWriter(保存文件名, 选择, Encoding.GetEncoding("GB2312"));
if (选择) 写入.Write(输出内容); else 写入.WriteLine(输出内容);
写入.Flush();
写入.Close();
if (文件.Length > 1000 * 1024)
文件.MoveTo(创建文件);
});
写入文本.Start();
写入文本.Join();
}
catch (Exception Ts)
{ Console.WriteLine("!" + Ts.ToString()); }
finally { }
}
static List<string> 读文本记录(string 文件路径)
{
List<string> 内容 = new List<string>();
if (Directory.Exists(文件路径.Remove(文件路径.LastIndexOf("\\"))))
{
FileInfo 文件 = new FileInfo(文件路径);
if (!文件.Exists)
{
FileStream 创建或覆盖 = File.Create(文件路径);
创建或覆盖.Close();
}
if (文件.Exists)
using (FileStream 打开 = new FileStream(文件路径, FileMode.Open))
{
using (StreamReader 读取 = new StreamReader(打开, 获取文本编码(打开, Encoding.GetEncoding("GB2312"))))
while (!读取.EndOfStream) 内容.Add(读取.ReadLine());
}
}
return 内容;
}
static Encoding 获取文本编码(FileStream 打开文件, Encoding 编码)
{
Encoding 获取编码 = 编码;
if (打开文件 != null && 打开文件.Length >= 2)
{
byte byte1 = 0, byte2 = 0, byte3 = 0;
long Seek位置 = 打开文件.Seek(0, SeekOrigin.Begin);//保存当前Seek位置
打开文件.Seek(0, SeekOrigin.Begin);
byte1 = Convert.ToByte(打开文件.ReadByte());
byte2 = Convert.ToByte(打开文件.ReadByte());
if (打开文件.Length >= 3)
byte3 = Convert.ToByte(打开文件.ReadByte());
if (byte1 == 0xFE && byte2 == 0xFF)
获取编码 = Encoding.BigEndianUnicode;
if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)
获取编码 = Encoding.Unicode;
if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)
获取编码 = Encoding.UTF8;
打开文件.Seek(Seek位置, SeekOrigin.Begin);//恢复Seek位置
}
return 获取编码;
}