当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Cannot delete file because it is being used by another process

问题描述:

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);

}

Towards the end where it says "Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);" i'm getting an error that it's not allowing me to delete the file. I'm GUESSING it's because something in the code is not closing the file or what not. does anyone know what could be the case? or how I would go about using "using" to fix this problem?

IOException was unhandled. The process cannot access the file 'blah blah' because it is being used by another process.

This is the code for 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++;

}

}

网友答案:
using(Bitmap pages = (Bitmap)Image.FromFile(SamePolicyPics[0])){
        int frame = 0;

        foreach (string picture in SamePolicyPics)
        {
            if (frame == 0)
            {
                pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7)  + ".tiff", info, ep);
            }
            else
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                using(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++;
        }}
网友答案:
Bitmap bm = (Bitmap)Image.FromFile(picture);

This will keep the file locked until bm is disposed, as the manual says. Wrap it in a using block:

using (Bitmap bm = (Bitmap)Image.FromFile(picture))
{
   // your code
}
网友答案:

It'll require some rewriting of your code cause the object will only exist inside the using scope. But the idea is something like this

foreach (string picture in SamePolicyPics)
{
    using (pages = (Bitmap)Image.FromFile(picture))
    {
        if (frame == 0)
        {
            pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7)  + ".tiff", info, ep);
        }
        else
        {
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
            using (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++;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: