【发布时间】:2012-11-11 20:53:10
【问题描述】:
我见过很多这样的问题已经解决,问题主要是由于流没有被正确处理。
我的问题略有不同,这里按照代码sn-p
foreach (Images item in ListOfImages)
{
newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension);
File.Create(newPath);
File.WriteAllBytes(newPath, item.File);
}
其中Images 是自定义结构,item.File 是原始数据字节[]。
我的问题是在调用WriteAllBytes 的那一行,抛出了一个异常。消息内容如下:
The process cannot access the file because it is being used by another process
再次,我不知道我将如何以某种方式 close 处理。
【问题讨论】:
-
某些图像是否具有相同的 ImageName 和 ImageExtension?
-
你确定,路径 newPath 存在吗?
-
删除
File.Create(newPath);并尝试。 File.WriteAllBytes:Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.msdn.microsoft.com/en-us/library/… -
您不处置由 File.Create 创建的流。所以它仍在使用该文件。蒂姆的答案是正确的
标签: c# asp.net file-in-use