Files
29WxOrder/AllBase/FileBase.cs
T
2026-06-11 15:17:50 +08:00

423 lines
12 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.IO;
using System.Text;
namespace AllBase
{
public static class FileBase
{
/// <summary>
/// 文件是否存在
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool Exists(string path)
{
try
{
return File.Exists(path);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadAllText(string path)
{
return ReadAllText(path, Encoding.Default);
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string ReadAllText(string path, Encoding encoding)
{
try
{
return File.ReadAllText(path, encoding);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return null;
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string[] ReadAllLines(string path, Encoding encoding)
{
try
{
return File.ReadAllLines(path, encoding);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return null;
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string[] ReadAllLines(string path)
{
return ReadAllLines(path, Encoding.Default);
}
/// <summary>
/// 写入文件
/// </summary>
/// <param name="path"></param>
/// <param name="content"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static bool WriteAllText(string path, string content, Encoding encoding)
{
try
{
File.WriteAllText(path, content, encoding);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
public static byte[] ReadBytes(string path)
{
try
{
return File.ReadAllBytes(path);
}
catch (Exception e)
{
LogSave.Add(e.Message);
}
return null;
}
public static bool WriteAllText(string path, string content)
{
return WriteAllText(path, content, Encoding.Default);
}
public static bool WriteBytes(string path, byte[] content)
{
try
{
File.WriteAllBytes(path, content);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
/// <summary>
/// 写入文件
/// </summary>
/// <param name="path"></param>
/// <param name="content"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static bool AppendAllText(string path, string content, Encoding encoding)
{
try
{
File.AppendAllText(path, content, encoding);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
public static bool AppendAllText(string path, string content)
{
return AppendAllText(path, content, Encoding.Default);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool Delete(string path)
{
try
{
File.Delete(path);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool DirectoryCreate(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
public static bool Move(string oldFile, string newFile)
{
try
{
File.Move(oldFile, newFile);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
/// <summary>
/// 将文本内容写入指定文本
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="fileName">文本名称</param>
/// <param name="content">文本内容</param>
/// <param name="isSetEmpty">是否将原来内容置空</param>
/// <param name="relativePath">是否是相对路径 ,默认相对路径</param>
public static void WriteTxt(string path, string fileName, string content, bool isSetEmpty, bool relativePath = true)
{
if (relativePath)
{
path = Environment.CurrentDirectory + path;
}
try
{
//判断文件路径是否存在
DirectoryInfo directory = new DirectoryInfo(path);
bool isHas = directory.Exists;
if (!isHas)//若文件路径不存在则创建
{
isHas = FileBase.DirectoryCreate(path);
}
if (isHas)
{
string file = fileName + ".txt";
string filePath = path + "\\" + file;
if (isSetEmpty)
{
FileStream fileStream = new FileStream(filePath, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.Default);
streamWriter.Write(content);
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
}
else
{
//FileStream fileStream = new FileStream(filePath, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(filePath, true);
streamWriter.Write(content);
streamWriter.Flush();
streamWriter.Close();
//fileStream.Close();
}
}
}
catch (Exception)
{
//throw;
}
}
public static System.Text.Encoding GetFileEncoding(string filename)
{
using (var fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var br = new System.IO.BinaryReader(fs))
{
var buffer = br.ReadBytes(2);
if (buffer[0] >= 0xEF)
{
if (buffer[0] == 0xEF && buffer[1] == 0xBB)
{
return System.Text.Encoding.UTF8;
}
if (buffer[0] == 0xFE && buffer[1] == 0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
if (buffer[0] == 0xFF && buffer[1] == 0xFE)
{
return System.Text.Encoding.Unicode;
}
}
return GetEncodingWithBomUtf8(fs, System.Text.Encoding.Default);
}
}
private static System.Text.Encoding GetEncodingWithBomUtf8(Stream fs, Encoding defaultEncoding)
{
byte[] unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] unicodeBig = new byte[] { 0xFE, 0xFF, 0x00 };
//带BOM
byte[] utf8 = new byte[] { 0xEF, 0xBB, 0xBF };
var reVal = defaultEncoding;
using (var r = new System.IO.BinaryReader(fs))
{
byte[] ss = r.ReadBytes(4);
if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
else
{
if (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)
{
reVal = Encoding.UTF8;
}
else
{
int i;
int.TryParse(fs.Length.ToString(), out i);
ss = r.ReadBytes(i);
if (IsUtf8Bytes(ss))
{
reVal = Encoding.UTF8;
}
}
}
return reVal;
}
}
private static bool IsUtf8Bytes(byte[] data)
{
int charByteCounter = 1;  //计算当前正分析的字符应还有的字节数
for (int i = 0; i < data.Length; i++)
{
var curByte = data[i]; //当前分析的字节.
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式!");
}
return true;
}
#region 从文本读取内容
public static string ReadTxt(string path, string fileName, bool relativePath = true)
{
string str = "";
if (relativePath)
{
path = Environment.CurrentDirectory + path;
}
DirectoryInfo directory = new DirectoryInfo(path);
if (directory.Exists)//存在,开始读
{
var dics = directory.GetFiles();
for (int k = 0; k < dics.Length; k++)//循环每个店铺
{
string file = dics[k].ToString();
if (!string.IsNullOrEmpty(fileName))
{
file = fileName;
}
string filePath = path + "\\" + file;
bool isHas = FileBase.Exists(filePath);
if (isHas)
{
str = ReadAllText(filePath);
}
}
}
return str;
}
#endregion
}
}