IIS Express Asp.Net正在使用该文件

本文关键字:文件 Express Asp Net IIS | 更新日期: 2023-09-27 18:23:46

我在asp.net应用程序中有一个映像,我对它进行了多次调整和转换,但有时它会被IIS Express进程使用。我无法调整大小或转换它。如何释放IIS Express句柄或如何绕过它?在发布之前使用IIS Express是否正常,或者我应该完全处理这种情况?

这段代码用于替换两个图像:

public static void Replace(string OldPicture, Stream NewPicture)
{
   Bitmap OldImage = new Bitmap(OldPicture); 
   ImageFormat format = OldImage.RawFormat;
   Bitmap NewImage = new Bitmap(NewPicture);
   OldImage.Dispose();
   NewImage.Save(OldPicture, format);
   NewImage.Dispose();
   return;
}

通过NewImage.save 保存时旧图片正在使用

IIS Express Asp.Net正在使用该文件

如果文件打开,就会发生这种情况。可能您必须调用.Dispose()或使用using(...){...}

处置示例:

Bitmap source = new Bitmap(filePath);
Rectangle croptRect = new Rectangle((int)cropX, (int)cropY, (int)resizeWidth, (int)resizeHeight);
Bitmap target = source.Clone(croptRect, source.PixelFormat);
source.Dispose();
// working ...
target.Dispose();

使用示例:

if (!File.Exists(_path)) using (File.Open(_path)) { }