Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Vb.net application has two buttons. I want both these buttons to write a wave file such that on clicking the second button, the content of the wave file, created by clicking on the first button, is overwritten. is it possible to do so? Can Wav files be overwritten? If yes, how?
I am trying but it gives "used by another process error".
Posted

Yes, they can be overwritten - but the problem you are having is that the code you wrote to write teh file in teh first button press (and probably the second as well) does not clean up after itself as it should.

If you open a stream (or other file related object) for writing, and do not Close and Dispose of it when you are finished, it remains open until the Garbage Collector decides it is no longer needed or your application ends. This could be next week for all you know!

Either use a Using block:
VB
Using MyFile As New FileStream()
   ...
End Using
Or call the Dispose Method when you are finished with writing the file.
 
Share this answer
 
Comments
Member 8244358 9-Oct-12 3:52am    
yes, i am using Filestream method and i am also closing the binarywriter and filestream using the following code:

binarywriter.close()
filestream.close()

But it is still giving me error!
OriginalGriff 9-Oct-12 4:00am    
Show the code for both buttons.
Member 8244358 9-Oct-12 12:00pm    
Yes, your suggestion worked for me. I am able to overwrite the two files now. I was only using
binarywriter.flush()
and not
binarywriter.close().
Now I am using both and it works fine.


My next question is how do I make Windows media player shift from playing the first wave file to another on clicking second button and back to first wave file on clicking first button, since the wave file name in both the cases is the same?
OriginalGriff 9-Oct-12 12:05pm    
This is from my C# code:
mediaPlayer.Ctlcontrols.stop();
mediaPlayer.URL = playingNowList[playingNow].Location;
lbPlayingNow.SelectedIndex = playingNow;
mediaPlayer.Ctlcontrols.play();
As you can see, I just stop any existing track play, set the URL of the player instance, and start a new one. Works fine for me! :laugh:
Member 8244358 10-Oct-12 11:39am    
so do you mean that I should create a playlist and each time add the same wave file, that I modify, to that playlist and then use your code? Am I right?
A wav file is just a file, of course it can be overwritten.
Well, "used by another process" could mean as well, that it is used by the same process - you might have an unclosed file handle. But you can figure it out who and what is using it using a tool like ProcessExplorer[^], Process Hacker[^] or Unlocker[^].
 
Share this answer
 
v2
  • Yes, WAV files may be overwritten (like any other file).
  • To overwrite a file just write on it.
  • "used by another process" means there is a sharing violation (e.g. another process is reading it while you issued the write operation).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900