This project has moved and is read-only. For the latest updates, please go here.

Copying files and "file is being used by another process" problem.

May 24, 2012 at 10:48 AM

Hi, I have created my own WaveViewer control. Actually this is th copied code with some additional possibilities.

One of them is cutting part of file. I do it this way:
1. I copy part of file before cut position to new stream
2. I copy part of file after cut position to new stream(this one in 1 point)
3. I save new stream as temporary file
4. I close both streams(the original and new - temporary)
5. I copy new temporary file to the old - original file
6. I delete temporary file.

When I cut selected part of wave for the first time, everything works fine. But when I cut another part(do the second cut), I get error: "the file is being used by another process" - meaning the original file - while copying.

This is my code:

public void CutSelection(string fileName)
{
//get start and end time of the selection
  TimeSpan startTime = GetSelectionStartTime();
  TimeSpan endTime = GetSelectionEndTime();
 
//change the times to position in bytes
  int bytesPerMilisecond = waveStream.WaveFormat.AverageBytesPerSecond / 1000;
  int startPos = (int)startTime.TotalMilliseconds * bytesPerMilisecond;
  startPos = startPos - startPos % waveStream.WaveFormat.BlockAlign;
 
  int endPos = (int)endTime.TotalMilliseconds * bytesPerMilisecond;
            endPos = endPos - endPos % waveStream.WaveFormat.BlockAlign;
 
//create new file - its name ends with  "_"
  using (WaveFileWriter writer = new WaveFileWriter(fileName+"_", waveStream.WaveFormat))
  {
      byte[] buffer = new byte[1024];
      int bytesRead = 0;
      int bytesRequired = 0;
 
//before
      waveStream.Position = 0;
      while (waveStream.Position < startPos)
      {
          bytesRequired = (int)(startPos - waveStream.Position);
          if (bytesRequired > 1024) bytesRequired = 1024;
 
          bytesRead = waveStream.Read(buffer, 0, bytesRequired);
          writer.Write(buffer, 0, bytesRead);
      }
 
//after
      waveStream.Position = endPos;
      while (waveStream.Position < waveStream.Length)
      {
          bytesRequired = (int)(waveStream.Length - waveStream.Position);
          if (bytesRequired > 1024) bytesRequired = 1024;
 
          bytesRead = waveStream.Read(buffer, 0, bytesRequired);
          writer.Write(buffer, 0, bytesRead);
      }
  }//using
 
//close the original file(fileName)
  waveStream.Close();
  waveStream.Dispose();
  waveStream = null;
 
//copy new file to the original - here is the error - only at second time
  File.Copy(fileName + "_", fileName, true);
 
//delete temporary file
  File.Delete(fileName + "_");
 
//create waveStream from new file
  waveStream = new WaveFileReader(fileName);
  
//and invalidate control
  this.Invalidate();
}

 

So why do I get this error? What's wrong?

May 25, 2012 at 10:16 AM

do you have a windows explorer window open? sometimes I find that Windows seems to examine new WAv files as they are created

May 28, 2012 at 10:45 AM
Edited May 28, 2012 at 10:50 AM

No, all explorer windows are closed. When I try to delete it from Windows, I get error message saying that this file is being used by vshost32.exe

Jun 11, 2012 at 4:47 PM

are you sure you're not trying to create the same file twice?