for (int i = 0; i < PrefixDistinctCount; i++)
{
string FirstPolicy = "";
FirstPolicy = OriginalPics[0].ToString().Substring(26, 7);
var SamePoliciesQuery = OriginalPics.Where(x => Path.GetFileName(x).StartsWith(FirstPolicy));
foreach (string picture in SamePoliciesQuery)
{
File.Move(picture, AppVars.ProcessingPolicyImagesDirectory + picture.Substring(26, 12) + ".jpg");
}
string[] SamePolicyPics = Directory.GetFiles(AppVars.ProcessingPolicyImagesDirectory);
GenerateTiffFile(SamePolicyPics);
Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);
Directory.CreateDirectory(AppVars.ProcessingPolicyImagesDirectory);
OriginalPics = Directory.GetFiles(SelectedDirectory);
}
到最后它说“Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);” 我收到一个错误,它不允许我删除文件。我猜这是因为代码中的某些内容没有关闭文件或没有关闭文件。有谁知道可能是什么情况?或者我将如何使用“使用”来解决这个问题?
IOException 未处理。该进程无法访问文件“blah blah”,因为它正被另一个进程使用。
这是 GenerateTiffFile 的代码 -
public void GenerateTiffFile(string[] SamePolicyPics)
{
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff")
info = ice;
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Bitmap pages = null;
int frame = 0;
foreach (string picture in SamePolicyPics)
{
if (frame == 0)
{
pages = (Bitmap)Image.FromFile(picture);
//pages.Save(picture.Substring(0, picture.Length - 3) + "tiff", info, ep);
pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep);
}
else
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
Bitmap bm = (Bitmap)Image.FromFile(picture);
pages.SaveAdd(bm, ep);
}
if (frame == SamePolicyPics.Length - 1)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}