We help IT Professionals succeed at work.

IOException was unhandled

cmdolcet used Ask the Experts™
I get an error every time I go and try to delete a file. The process cannot access the file because it is being used by another process.

I open the file up and make some changes. I wanted to update the file with the new file so I delete the old file and save the new file. I have a screen shot of the error and the two routines that do what I stated above.

'Saves the .XML file
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
        Dim FileCheckDates As String

        If ValidateGridControl() = False Then
            btnSaveActive = True
            FileCheckDates = File_Selected_No_Extensions
            FileCheckDates = System.IO.Path.GetFileNameWithoutExtension(FileCheckDates)
            If FileCheckDates Like "*########" Then
                FileCheckDates = FileCheckDates.Remove(FileCheckDates.Length - 12)
            End If

            _partfile.Save(_dir_name_XML_Files & FileCheckDates & ".xml")
        End If
    End Sub  

'Save - saves the part file to a file
    Public Sub Save(ByVal filename As String)
        'do we need to delete the file
        Dim info As New FileInfo(filename)
        If info.Exists Then
        End If

        'create the new file
        Dim stream As New FileStream(filename, FileMode.Create)

        'save it
        Dim serializer As New XmlSerializer(Me.GetType)
        serializer.Serialize(stream, Me)

        'close the file
    End Sub

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

You need to either use a stream to open the file and use the stream to make changes. Or save the new file with a temp name, close original file, delete, and then rename the temp file with old name.
Miguel OzSenior Software Engineer
Top Expert 2009

Replace this:
Dim info As New FileInfo(filename)
        If info.Exists Then
        End If

        If File.Exists(filename) Then
        End If
Just make sure the file is not open by your application or any other application. As you are serializing the object you can keep all your changes in memory and save them to file when you are ready.

Note: 1) Also use try.. finally to close the stream to guarantee you clean the stream resource.
2) If not successful, Is your app multi-threading?


mas_oz2003: I tried and replaced my line with your suggested code and it still give me the same error message. How can I check to make sure the file is or isn;t open and if it is close it?

Also Code cruiser suggested making a temp file how can I do this?
Miguel OzSenior Software Engineer
Top Expert 2009

Use process explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653) and make sure your program is the only one opening the xml file. (If you open this file in notepad or VS your program will fail because the file is being used by another program)

You are trying to delete a file that you still have a reference to it, you must check all your code to see if you are closing all the streams related to your file, before attempting to save. Your overall design should be: Load file to memory object(and close the stream), modify object in memory, save object to file.

Even if you build a temp file this code will still fail as you would not be able to replace the existing open file.


how do I close my stream once its open ?

this is how I open my file.

 Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(SelectedFile_Arr(0))
        File_Selected_No_Extensions = System.IO.Path.GetFileNameWithoutExtension(SelectedFile_Arr(0))
Senior Software Engineer
Top Expert 2009
the using clause will force the reader to de the cleanup.

or use the close method: (http://msdn.microsoft.com/en-US/library/sbssxwha(v=VS.80).aspx)
after you finish with the reader.