1

I would like to tag some existing mp3 with taglib#. I have the following error message: "The process cannot access the file because it is being used by another process." I don't know what kind of process it can be. I can access any mp3 files on any of my hard drives, I also can use the properties of the file, but I cannot save changes.

This is the code.

OpenFileDialog f = new OpenFileDialog();
if ((bool)f.ShowDialog())
{
   try
   {
       if ( f.OpenFile() != null)
       {

       TagLib.File file = TagLib.File.Create(f.FileName);                            
       file.Tag.Album = "Album1";
       file.Save();

       }
   }
   catch (Exception ex)
   {
     MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
   }
}
else
{
    MessageBox.Show("Problem occured, try again later");
}

Could you help me? Thanks

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
  • If that's really your error, your question is more of finding out what's using the MP3 file. Does http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net and http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c help? Unless, you're not opening the Stream with Write access? It's odd that unused MP3 are being used. – Jason Mar 16 '12 at 23:11
  • 1
    `f.OpenFile()` will never return `null`. – SLaks Mar 16 '12 at 23:13
  • Why do you think clicking Cancel means `Problem occured`? – SLaks Mar 16 '12 at 23:14

1 Answers1

2

f.OpenFile() creates a FileStream around the file.
Since you never close this stream, the file remains open.

Don't do that.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964