Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Why am I getting "the process cannot access the file * because it is being used by another process" with this code?

I'm trying to convert bmp files in a folder to jpg, then delete the old files. The code works fine, except it can't delete the bmp's.

DirectoryInfo di = new DirectoryInfo(args[0]);
FileInfo[] files = di.GetFiles("*.bmp");
foreach (FileInfo file in files)
{
    string newFile = file.FullName.Replace("bmp", "jpg");
    Bitmap bm = (Bitmap)Image.FromFile(file.FullName);
    bm.Save(newFile, ImageFormat.Jpeg);
}
for (int i = 0; i < files.Length; i++)
    files[i].Delete();

The files aren't being used by another program/process like the error indicates, so I'm assuming the problem is here. But to me the code seems fine, since I'm doing everything sequentially. This is all that there is to the program too, so the error can't be caused by code elsewhere.

like image 653
zxcvbnm Avatar asked Mar 19 '10 18:03

zxcvbnm


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


3 Answers

Try wrap bitmaps with using:

using (Bitmap bm = (Bitmap)Image.FromFile(file.FullName))
{
    bm.Save(newFile, ImageFormat.Jpeg);
}

This will dispose bitmap objects just after they were saved.

like image 109
Andrew Bezzub Avatar answered Sep 29 '22 15:09

Andrew Bezzub


The best way to solve the issue with Image.FromFile wherein it leaves file handles open is to use Image.FromStream instead.

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
   using (Image original = Image.FromStream(fs))
   {
     ...

Using an explicit Dispose(), a using() statement or setting the value to null doesn't solve the issue until a garbage collection happens (and forcing a garbage collection to happen is generally a bad idea).

like image 21
Ian Mercer Avatar answered Sep 29 '22 16:09

Ian Mercer


public static Image LoadImage( string fileFullName )
{
    Stream fileStream = File.OpenRead( fileFullName );
    Image image       = Image.FromStream( fileStream );

    // PropertyItems seem to get lost when fileStream is closed to quickly (?); perhaps
    // this is the reason Microsoft didn't want to close it in the first place.
    PropertyItem[] items = image.PropertyItems;

    fileStream.Close();

    foreach ( PropertyItem item in items )
    {
        image.SetPropertyItem( item );
    }

    return image;
}
like image 41
Erlend Robaye Avatar answered Sep 29 '22 15:09

Erlend Robaye