【发布时间】:2018-09-12 18:35:16
【问题描述】:
我正在做一个 c# wcf 服务,在该服务中我收到一堆图像,该服务将它们合并到一个多图像 Tiff 文件中。在服务结束时,我想删除原始文件,但我收到一个错误,指出其他进程正在锁定文件。
这是接收图像(作为 byte[] 列表)并将它们写入磁盘的代码
public static List<string> SaveByteImagesToFile(List<byte[]> bytesToCopyIntoFiles, string imageReferenceType, string imageReferenceValue)
{
_applicationLogger.Debug(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name);
string imageFinalPath = string.Empty;
string joinImagesFilePath = string.Empty;
List<string> imagesFilePath = new List<string>();
int count = 1;
try
{
if (bytesToCopyIntoFiles.Count == 0)
{
throw new ArgumentNullException("bytesToCopyIntoFiles");
}
else
{
joinImagesFilePath = SettingsManager.GetServiceSetting(AppSettingsKeys.CopyImagesToFilePath, "NO_VALID_FILEPATH");
if (joinImagesFilePath.IsValidFilePath(out string errorMessage, true, true))
{
foreach (byte[] image in bytesToCopyIntoFiles)
{
var imageFileName = imageReferenceType + "_" + imageReferenceValue + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + count.ToString();
imageFinalPath = joinImagesFilePath + Path.DirectorySeparatorChar + imageFileName + ".tiff";
using (FileStream stream = new FileStream(imageFinalPath, FileMode.Create, FileAccess.ReadWrite))
{
stream.Write(image, 0, image.Length);
stream.Flush();
}
imagesFilePath.Add(imageFinalPath);
count++;
}
}
else
{
exceptionMessageType = MainRepository.GetExceptionMessage("E171");
throw new IOException(exceptionMessageType.ExceptionMessage + " " + errorMessage);
}
}
return imagesFilePath;
}
catch
{
throw;
}
}
我可以如何或使用什么来阻止服务或任何进程锁定文件。如您所见,我正在使用文件流的 using 范围,但没有任何运气。
有什么想法吗?谢谢
【问题讨论】:
-
有时病毒扫描程序会导致此问题。尝试进程监视器以查看谁/什么正在访问该文件
-
iis worker 是锁定文件的那个。这就是我在进程监视器中看到的