123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace BZFAStandardLib
- {
- /// <summary>
- /// txt文件操作专用类
- /// </summary>
- public static class FileOperate
- {
- private static Mutex mutexNew = new Mutex();
- private static Mutex mutexWrite = new Mutex();
- /// <summary>
- /// 新建文件,
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- public static void NewTxtFile(string txtFile)
- {
- string path = txtFile.Substring(0, txtFile.LastIndexOf("\\"));
- if (!Directory.Exists(path)) Directory.CreateDirectory(path); //判断路径是否存在,不存在则创建
- if (!File.Exists(txtFile)) //判断文件是否存在,不存在则创建
- {
- using (FileStream fileStream = new FileStream(txtFile, FileMode.Create, FileAccess.Write))
- {
- fileStream.Close();
- }
- }
- }
- /// <summary>
- /// 新建并写入txt文件;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- /// <param name="txt">写入内容</param>
- public static void NewTxtFile(string txtFile, string txt)
- {
- string str1 = "\r\n====================================================================\r\n";
- string str2 = "\r\n====================================================================";
- string path = txtFile.Substring(0, txtFile.LastIndexOf("\\"));
- bool fileExists = File.Exists(txtFile);
- // 如果文件不存在,则创建文件并写入内容
- if (!fileExists)
- {
- using (FileStream fileStream = new FileStream(txtFile, FileMode.Create, FileAccess.Write))
- using (StreamWriter streamWriter = new StreamWriter(fileStream))
- {
- streamWriter.Write(str1 + txt + str2); // 直接写入内容
- }
- }
- else
- {
- // 如果文件存在,则打开文件并在末尾追加内容
- using (FileStream fileStream = new FileStream(txtFile, FileMode.Append, FileAccess.Write))
- using (StreamWriter streamWriter = new StreamWriter(fileStream))
- {
- streamWriter.WriteLine(str1 + txt + str2); // 使用 WriteLine 确保有换行符
- }
- }
- }
- /// <summary>
- /// 带锁
- /// </summary>
- /// <param name="txtFile"></param>
- public static void NewFile(string file)
- {
- try
- {
- mutexNew.WaitOne();
- string path = file.Substring(0, file.LastIndexOf("\\"));
- if (!Directory.Exists(path)) Directory.CreateDirectory(path); //判断路径是否存在,不存在则创建
- if (!File.Exists(file)) //判断文件是否存在,不存在则创建
- {
- using (FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write))
- {
- fileStream.Close();
- }
- }
- mutexNew.ReleaseMutex();
- }
- catch (Exception ex)
- {
- mutexNew.ReleaseMutex();
- }
- }
- /// <summary>
- /// 写入txt文件;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- /// <param name="txt">写入内容</param>
- public static bool WriteTxtFile(string txtFile, string txt)
- {
- if (!File.Exists(txtFile)) NewTxtFile(txtFile); //判断文件是否存在,不存在则新建;
- try
- {
- using (FileStream fileStream = new FileStream(txtFile, FileMode.Open, FileAccess.Write)) //写入文件
- {
- StreamWriter streamWriter = new StreamWriter(fileStream);
- //sw.BaseStream的Position或Seek()可移动文件流指针到的任意位置;
- streamWriter.BaseStream.Seek(0, SeekOrigin.End); //在txt末尾写入;
- streamWriter.Write(txt); //写入txt文件内容;
- streamWriter.Flush();
- streamWriter.Close();
- fileStream.Close();
- }
- return true;
- }
- catch { return false; }
- }
- /// <summary>
- /// 带锁
- /// </summary>
- /// <param name="txtFile"></param>
- /// <param name="txt"></param>
- public static void WriteFile(string file, string txt)
- {
- try
- {
- mutexWrite.WaitOne();
- if (!File.Exists(file)) NewFile(file); //判断文件是否存在,不存在则新建;
- using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Write)) //写入文件
- {
- StreamWriter streamWriter = new StreamWriter(fileStream);
- //sw.BaseStream的Position或Seek()可移动文件流指针到的任意位置;
- streamWriter.BaseStream.Seek(0, SeekOrigin.End); //在txt末尾写入;
- streamWriter.Write(txt); //写入txt文件内容;
- streamWriter.Flush();
- streamWriter.Close();
- fileStream.Close();
- }
- mutexWrite.ReleaseMutex();
- }
- catch
- {
- mutexWrite.ReleaseMutex();
- }
- }
- /// <summary>
- /// 读取txt文件内容;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- /// <returns>返回txt文件内容</returns>
- public static string ReadTxtFile(string txtFile)
- {
- if (!File.Exists(txtFile)) return ""; //判断文件是否存在;
- string Return = "";
- using (FileStream fileStream = new FileStream(txtFile, FileMode.Open, FileAccess.Read))
- {
- StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8);
- Return = streamReader.ReadToEnd().ToString();
- streamReader.Close();
- fileStream.Close();
- }
- return Return;
- }
- /// <summary>
- /// 读取最后一行内容;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- /// <returns></returns>
- public static string ReadLastLine(string txtFile)
- {
- if (!File.Exists(txtFile)) return ""; //判断文件是否存在;
- string[] allLines = File.ReadAllLines(txtFile);
- if (allLines.Length > 0)
- return allLines[allLines.Length - 1];
- else return "";
- }
- /// <summary>
- /// 覆盖最后一行内容;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- /// <param name="txt"></param>
- public static void CoverLastLine(string txtFile, string txt)
- {
- if (File.Exists(txtFile)) //判断文件是否存在;
- {
- string[] allLines = File.ReadAllLines(txtFile);
- if (allLines.Length > 0)
- {
- allLines[allLines.Length - 1] = txt;
- File.WriteAllLines(txtFile, allLines);
- }
- }
- }
- /// <summary>
- /// 移除最后一行内容;
- /// </summary>
- /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
- public static void RemoveLastLine(string txtFile)
- {
- if (File.Exists(txtFile)) //判断文件是否存在
- {
- string[] allLines = File.ReadAllLines(txtFile);
- if (allLines.Length > 0)
- {
- Array.Resize(ref allLines, allLines.Length - 1);
- File.WriteAllLines(txtFile, allLines);
- }
- }
- }
- }
- }
|