Results 1 to 7 of 7

Thread: Accessing same file from multiple instances of application

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Accessing same file from multiple instances of application

    I have noticed alot of "Cannot access file because it is being used by another process" errors in my bug tracker reports. I am geussing this might have to do with the file not being closed after it has been opened. Or not being closed after a save. Can anyone verify if this is my issue and advise me a better way to go about it.

    On form load this file is opened, and on close the file is saved.

    Form Load

    Code:
    If IO.File.Exists(myCoolFile) Then '// check if file exists.
                Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file as a string array.
                For Each line As String In myCoolFileLines '// loop thru array list.
                    Dim lineArray() As String = line.Split("#") '// separate by "#" character.
                    'Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
                    ' ListView1.Items.Add(newItem) '// add Item to ListView.
                    ListView1.Items.Add(lineArray(0)).Tag = (lineArray(1))
                Next
    
            Else
                If Not File.Exists(myCoolFile) Then
                    File.Create(myCoolFile)
                    End If
    Form Close

    Code:
    Dim myWriter As New IO.StreamWriter(myCoolFile)
            For Each myItem As ListViewItem In ListView1.Items
                myWriter.WriteLine(myItem.Text & "#" & myItem.Tag) '// write Item and SubItem.
            Next
            myWriter.Close()

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Accessing same file from multiple instances of application

    Something I notice right away in the form_load is the if/else/end if statement. It should look like this:
    Code:
    If IO.File.Exists(myCoolFile) Then
    'Code here for if the file exist
    Else
    'Code here for if the file DOES NOT exist
    End If
    Simply because File.Exist returns a boolean value and with the if/else/end if statement, if the boolean isn't true, then the else could only be false.

    As for the file.create in your else statement, it returns a FileStream. The FileStream gets created so you can write to the file you just created. It's the FileStream that you need to close.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Accessing same file from multiple instances of application

    Quote Originally Posted by dday9 View Post
    Something I notice right away in the form_load is the if/else/end if statement. It should look like this:
    Code:
    If IO.File.Exists(myCoolFile) Then
    'Code here for if the file exist
    Else
    'Code here for if the file DOES NOT exist
    End If
    Simply because File.Exist returns a boolean value and with the if/else/end if statement, if the boolean isn't true, then the else could only be false.

    As for the file.create in your else statement, it returns a FileStream. The FileStream gets created so you can write to the file you just created. It's the FileStream that you need to close.

    Hi dday, were you referring to the missing end if in the code I posted? I must have left it out when I was copying the code.

    How to I close the filestream in my else statement?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Accessing same file from multiple instances of application

    I don't really understand the logic here anyway. If the file exists at form load then read it. If it doesn't create it? Why? You don't need it until you write to it which apparently isn't until the form closes.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Accessing same file from multiple instances of application

    True, the reason why it was on form load was because in my application you could save the data to the file while it was open instead of close. Even then it still doesn't really make sense though I geuss. So I have changed it to create the file on save. Thanks.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Accessing same file from multiple instances of application

    Actually you don't have to create the file if it doesn't exist. Creating the StreamWriter as you did here would do so if it doesn't already exist:-
    vbnet Code:
    1. Dim myWriter As New IO.StreamWriter(myCoolFile)
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Accessing same file from multiple instances of application

    Thanks I had no idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width