418 lines
12 KiB
C#
418 lines
12 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Text;
|
|||
|
|
using CYQ.Data.Cache;
|
|||
|
|
using System.Threading;
|
|||
|
|
namespace CYQ.Data.Tool
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ļ<EFBFBD><C4BC><EFBFBD>ȡ<EFBFBD>ࣨ<EFBFBD><E0A3A8><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>룩
|
|||
|
|
/// </summary>
|
|||
|
|
public static partial class IOHelper
|
|||
|
|
{
|
|||
|
|
private static DistributedCache _cache = null;
|
|||
|
|
private static DistributedCache cache
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (_cache == null)
|
|||
|
|
{
|
|||
|
|
_cache = DistributedCache.Local;
|
|||
|
|
}
|
|||
|
|
return _cache;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
internal static Encoding DefaultEncoding = Encoding.Default;
|
|||
|
|
|
|||
|
|
#region ReadAllText
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"><3E><><EFBFBD><EFBFBD>·<EFBFBD><C2B7></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string ReadAllText(string fileName)
|
|||
|
|
{
|
|||
|
|
return ReadAllText(fileName, 0, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
/// <param name="cacheMinutes"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0<CEAA><EFBFBD><F2B2BBBB>棩</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string ReadAllText(string fileName, int cacheMinutes)
|
|||
|
|
{
|
|||
|
|
return ReadAllText(fileName, cacheMinutes, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="encoding">ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>⣩</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string ReadAllText(string fileName, int cacheMinutes, Encoding encoding)
|
|||
|
|
{
|
|||
|
|
return ReadAllText(fileName, cacheMinutes, encoding, 3);
|
|||
|
|
}
|
|||
|
|
private static string ReadAllText(string fileName, int cacheMinutes, Encoding encoding, int tryCount)
|
|||
|
|
{
|
|||
|
|
string key = "IO_" + fileName.GetHashCode();
|
|||
|
|
if (cache.Contains(key)) { return cache.Get<string>(key); }
|
|||
|
|
Byte[] buff = ReadAllBytes(fileName);
|
|||
|
|
string result = BytesToText(buff, encoding);
|
|||
|
|
if (cacheMinutes > 0)
|
|||
|
|
{
|
|||
|
|
cache.Set(key, result, cacheMinutes);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region ReadLines
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static string[] ReadAllLines(string fileName)
|
|||
|
|
{
|
|||
|
|
return ReadAllLines(fileName, 0, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
public static string[] ReadAllLines(string fileName, int cacheMinutes)
|
|||
|
|
{
|
|||
|
|
return ReadAllLines(fileName, cacheMinutes, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
public static string[] ReadAllLines(string fileName, int cacheMinutes, Encoding encoding)
|
|||
|
|
{
|
|||
|
|
string result = ReadAllText(fileName, cacheMinutes, encoding);
|
|||
|
|
if (!string.IsNullOrEmpty(result))
|
|||
|
|
{
|
|||
|
|
return result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region ReadAllBytes
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡIO<49><4F><EFBFBD>ݡ<EFBFBD><DDA1><EFBFBD>Mutex<65><78><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static byte[] ReadAllBytes(string fileName)
|
|||
|
|
{
|
|||
|
|
byte[] buff = null;
|
|||
|
|
var mutex = GetMutex(fileName);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (File.Exists(fileName))
|
|||
|
|
{
|
|||
|
|
buff = File.ReadAllBytes(fileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt(e);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return buff;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<F2B8B4B8><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"></param>
|
|||
|
|
/// <param name="text"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Write(string fileName, string text)
|
|||
|
|
{
|
|||
|
|
return Save(fileName, text, false, true, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
public static bool Write(string fileName, string text, Encoding encode)
|
|||
|
|
{
|
|||
|
|
return Save(fileName, text, false, true, encode);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><D7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"></param>
|
|||
|
|
/// <param name="text"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool Append(string fileName, string text)
|
|||
|
|
{
|
|||
|
|
return Save(fileName, text, true, true, DefaultEncoding);
|
|||
|
|
}
|
|||
|
|
public static bool Append(string fileName, string text, Encoding encode)
|
|||
|
|
{
|
|||
|
|
return Save(fileName, text, true, true, encode);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static Encoding GetEncoding(string fileName, Encoding defaultEncoding)
|
|||
|
|
{
|
|||
|
|
string key = "IOEncoding" + fileName.GetHashCode();
|
|||
|
|
if (cache.Contains(key))
|
|||
|
|
{
|
|||
|
|
return cache.Get<Encoding>(key);
|
|||
|
|
}
|
|||
|
|
string folder = Path.GetDirectoryName(fileName);
|
|||
|
|
if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(folder);
|
|||
|
|
}
|
|||
|
|
else if (File.Exists(fileName))
|
|||
|
|
{
|
|||
|
|
TextEncodingDetect detect = new TextEncodingDetect();
|
|||
|
|
byte[] bytes = ReadAllBytes(fileName);
|
|||
|
|
Encoding detectEncode = detect.GetEncoding(bytes, defaultEncoding);
|
|||
|
|
if (detectEncode != Encoding.ASCII)
|
|||
|
|
{
|
|||
|
|
defaultEncoding = detectEncode;
|
|||
|
|
}
|
|||
|
|
cache.Set(key, defaultEncoding, 5);
|
|||
|
|
}
|
|||
|
|
return defaultEncoding;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
internal static bool Save(string fileName, string text, bool isAppend, bool writeLogOnError, Encoding encode)
|
|||
|
|
{
|
|||
|
|
if (isAppend)
|
|||
|
|
{
|
|||
|
|
encode = GetEncoding(fileName, encode);
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (!isAppend)
|
|||
|
|
{
|
|||
|
|
string folderPath = Path.GetDirectoryName(fileName);
|
|||
|
|
if (!Directory.Exists(folderPath))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(folderPath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
if (writeLogOnError)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var mutex = GetMutex(fileName);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (StreamWriter writer = new StreamWriter(fileName, isAppend, encode))
|
|||
|
|
{
|
|||
|
|
//if (!isAppend && fileName.EndsWith(".txt"))
|
|||
|
|
//{
|
|||
|
|
// //д<><D0B4>bomͷ
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
writer.Write(text);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
if (writeLogOnError)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool Exists(string fileName)
|
|||
|
|
{
|
|||
|
|
return File.Exists(fileName);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ɾ<><C9BE><EFBFBD>ļ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool Delete(string fileName)
|
|||
|
|
{
|
|||
|
|
var mutex = GetMutex(fileName);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (File.Exists(fileName))
|
|||
|
|
{
|
|||
|
|
File.Delete(fileName);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static Mutex GetMutex(string fileName)
|
|||
|
|
{
|
|||
|
|
string key = "IO" + fileName.GetHashCode();
|
|||
|
|
var mutex = new Mutex(false, key);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
mutex.WaitOne();
|
|||
|
|
}
|
|||
|
|
catch (AbandonedMutexException ex)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӹرգ<D8B1>δ<EFBFBD>ͷż<CDB7><C5BC>˳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD>ͷ<EFBFBD>1<EFBFBD>μ<EFBFBD><CEBC>ɡ<EFBFBD><C9A1><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
mutex.WaitOne();
|
|||
|
|
}
|
|||
|
|
return mutex;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
internal static bool IsLastFileWriteTimeChanged(string fileName, ref DateTime compareTime)
|
|||
|
|
{
|
|||
|
|
bool isChanged = false;
|
|||
|
|
IOInfo info = new IOInfo(fileName);
|
|||
|
|
if (info.Exists && info.LastWriteTime != compareTime)
|
|||
|
|
{
|
|||
|
|
isChanged = true;
|
|||
|
|
compareTime = info.LastWriteTime;
|
|||
|
|
}
|
|||
|
|
return isChanged;
|
|||
|
|
}
|
|||
|
|
internal static string BytesToText(byte[] buff, Encoding encoding)
|
|||
|
|
{
|
|||
|
|
if (buff == null || buff.Length == 0) { return ""; }
|
|||
|
|
TextEncodingDetect detect = new TextEncodingDetect();
|
|||
|
|
encoding = detect.GetEncoding(buff, encoding);
|
|||
|
|
if (detect.hasBom)
|
|||
|
|
{
|
|||
|
|
if (encoding == Encoding.UTF8)
|
|||
|
|
{
|
|||
|
|
return encoding.GetString(buff, 3, buff.Length - 3);
|
|||
|
|
}
|
|||
|
|
if (encoding == Encoding.Unicode || encoding == Encoding.BigEndianUnicode)
|
|||
|
|
{
|
|||
|
|
return encoding.GetString(buff, 2, buff.Length - 2);
|
|||
|
|
}
|
|||
|
|
if (encoding == Encoding.UTF32)
|
|||
|
|
{
|
|||
|
|
return encoding.GetString(buff, 4, buff.Length - 4);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return encoding.GetString(buff);
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static partial class IOHelper
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ⲣ<EFBFBD><E2B2A2><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>롣
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"><3E>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static Encoding DetectEncode(string fileName)
|
|||
|
|
{
|
|||
|
|
TextEncodingDetect detect = new TextEncodingDetect();
|
|||
|
|
return detect.GetEncoding(File.ReadAllBytes(fileName));
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>Ⲣ<EFBFBD><E2B2A2><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>롣
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="bytes"><3E><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static Encoding DetectEncode(byte[] bytes)
|
|||
|
|
{
|
|||
|
|
TextEncodingDetect detect = new TextEncodingDetect();
|
|||
|
|
return detect.GetEncoding(bytes);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ļ<EFBFBD><C4BC>в<EFBFBD><D0B2><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static partial class IOHelper
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool ExistsDirectory(string path)
|
|||
|
|
{
|
|||
|
|
var mutex = GetMutex(path);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
return Directory.Exists(path);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ɾ<><C9BE><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool DeleteDirectory(string path, bool recursive)
|
|||
|
|
{
|
|||
|
|
var mutex = GetMutex(path);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Directory.Delete(path, recursive);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static string[] GetFiles(string path)
|
|||
|
|
{
|
|||
|
|
return GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
|
|||
|
|
{
|
|||
|
|
var mutex = GetMutex(path);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
return Directory.GetFiles(path, searchPattern, searchOption);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// <20>ͷŻ<CDB7><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
mutex.ReleaseMutex();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|