提问
如何读取被占用文件内容
解答
public override (bool ok, string msg) Excute()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
$@"Logs\Info\{DateTime.Now.ToString("yyyy-MM-dd")}.log");
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int fsLen = (int)fs.Length;
byte[] heByte = new byte[fsLen];
_ = fs.Read(heByte, 0, heByte.Length);
var log = System.Text.Encoding.GetEncoding("GB2312").GetString(heByte);
return (true, log);
}
return (true, $"没有找到日志文件{path}");
}