kimiraikkonen said:
Hi again,
I've tried all of code recommendations such as adding "close" and
"dispose" to stream writer and reader, the "this file is bein g used
by another process" problem occurs if you save and overwrite
existing file too fast after opening it. For example, consider that
you opened a text file with stream reader with no problem, then if
you try to save (overwrite) it in 1-2 seconds you get this error.
Still i don't know if there's no bug in streamwriter for overwriting
existing files, although i tried all of close, dispose , if nothing
combinations, why i got this notification.
Can you try this in a new empty project? (change the path in the first line
before)
Public Class Form1
Const Path As String = "g:\test.bin"
Friend WithEvents Button1 As System.Windows.Forms.Button
Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Button1 = New System.Windows.Forms.Button
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.Text = "Button1"
Me.Controls.Add(Button1)
End Sub
Shared Sub WriteFile()
Dim fs As New IO.FileStream(Path, IO.FileMode.Create)
Dim sw As New IO.StreamWriter(fs)
sw.WriteLine("bla")
sw.Dispose()
fs.Close()
End Sub
Shared Sub ReadFile()
Dim fs As New IO.FileStream(Path, IO.FileMode.OpenOrCreate)
Dim sr As New IO.StreamReader(fs)
sr.ReadLine()
sr.Dispose()
fs.Close()
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim sw As New Stopwatch
Dim count As Integer
sw.Start()
Do
If Rnd() > 0.5 Then
ReadFile()
Else
WriteFile()
End If
count += 1
Loop Until sw.Elapsed.TotalSeconds > 60
MsgBox(count.ToString("#,###"))
End Sub
End Class
I don't get an error even though files are closed/opened/read/written about
200,000 times a minute.
Do you get an error?
Armin