File is in use by another process.

wavemasta

Member
Joined
Feb 12, 2009
Messages
12
Programming Experience
10+
Hi all:

I am trying to generate a file.

I have a sub called generateHeaders(). That is responsible for generating header info for the file.
I am supposed to then write other info to that file. This is done in the sub generateInsertFile(). In that sub, I open a streamwriter to that same file in the generateHeader() sub, and append the neccessary info after the header.
Thing is I get this very annoying IOEXception :'The process cannot access the File 'insert_item.qli' because its in use by another process'.
I tried closing the streamwriter after using it, but it didn't work.
Any ideas for solving this? Its quite an annoying exception :).

Heres my code (VB.net 2008 express)
VB.NET:
Sub generateHeaders()
       
        'generate header for insert_item.qli
        'this is the file which gives the exception, but its not thrown here
        Dim Iwriter As New StreamWriter(configFileDir & "\insert_item.qli")
        Iwriter.WriteLine("@fields")
        Iwriter.WriteLine("@ITEM_DF")
        Iwriter.Close()
    End Sub
VB.NET:
    Sub generateInsertFile(ByVal tname2 As String, ByVal line2 As String())
        Try
            'At this point, that annoying exception is thrown
            Dim insertwriter As New StreamWriter(configFileDir & "\insert_item.qli")
            insertwriter.WriteLine(tname2)
            Dim i As Integer
            For i = 3 To line2.Length - 2
                insertwriter.Write("\" & line2(i) & "\" & ",")
            Next
            'write the last line
            insertwriter.Write("\" & line2(line2.Length - 1) & "\")
            insertwriter.WriteLine()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub
 
You have to close insertwriter in generateInsertFile method, always close a StreamWriter when you are done with it. You also have to specify append parameter for the insertwriter with the StreamWriter constructor if you intend to append.

Depending on how these methods are used it may be better to declare the StreamWriter in the calling method and just pass it to the writer methods, for example if generateInsertFile is to be called repeatedly it would be wasteful to open and close a StreamWriter for each call.
 
Back
Top