-
Nov 27th, 2012, 04:22 PM
#1
Thread Starter
Addicted Member
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()
-
Nov 27th, 2012, 05:15 PM
#2
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.
-
Nov 27th, 2012, 06:25 PM
#3
Thread Starter
Addicted Member
Re: Accessing same file from multiple instances of application
Originally Posted by dday9
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?
-
Nov 27th, 2012, 06:39 PM
#4
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!
-
Nov 27th, 2012, 07:36 PM
#5
Thread Starter
Addicted Member
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.
-
Nov 27th, 2012, 10:16 PM
#6
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:
Dim myWriter As New IO.StreamWriter(myCoolFile)
-
Nov 27th, 2012, 11:16 PM
#7
Thread Starter
Addicted Member
Re: Accessing same file from multiple instances of application
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|