【发布时间】:2014-01-30 10:00:15
【问题描述】:
我正在尝试在文件关闭后附加文件的 MD5 哈希(基于文件的长度)。我是这样做的:
string filePath = "myPath";
string fileName = "myFileName";
File.Delete(filePath + fileName);
if (!File.Exists(filePath + fileName))
{
using (var sw = File.CreateText(filePath + fileName))
{
sw.Write("Stuff to write");
}
}
using (var sw = File.AppendText(filePath + fileName))
{
sw.Write(ctx.GetMD5HashFromFile(filePath, fileName));
}
不幸的是,这不起作用,因为文件在两个 using 语句之间没有正确关闭。我收到以下错误:
Unhandled Exception: System.IO.IOException: The process cannot access the file '
[filePath + fileName] because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
如何正确计算 MD5 哈希并附加文本而不会出现异常?
【问题讨论】:
标签: c# file md5 streamwriter