|
-
Jul 29th, 2009, 12:39 PM
#1
Thread Starter
Member
[RESOLVED] Accessing text files
In this program i am making, i want to get information from a text file. but i need to get it from 2 different subs. one sub loops and gets the information every loop and other gets it when a user clicks a button. when they get it at the same time, i get an error saying that it is already being used by another process.
-
Jul 29th, 2009, 12:59 PM
#2
Re: Accessing text files
Probably you forget to close the stream after finishing reading the file.
Show us your code.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 29th, 2009, 01:00 PM
#3
Addicted Member
Re: Accessing text files
Why do you need to allow the user to get the information manually when one sub is constantly looping and getting the information?
-
Jul 29th, 2009, 03:18 PM
#4
Thread Starter
Member
Re: Accessing text files
Im not using a stream to access the file. Im just using IO.File.ReadAllText. I need to get the information 2 times because the loop writes the information using graphics.drawstring and the other sub changes the text in the file.
-
Jul 29th, 2009, 03:26 PM
#5
Re: Accessing text files
Can you post the relevant code instead of having us trying to guess what the problem is?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 29th, 2009, 04:05 PM
#6
Thread Starter
Member
Re: Accessing text files
Code:
If Not IO.File.Exists("C:\Text.txt") Then
IO.File.WriteAllText("C:\Text.txt", 10)
Else
IO.File.WriteAllText("C:\Text.txt", IO.File.ReadAllText("C:\Text.txt") - 1)
End If
Code:
Do
Me.CreateGraphics.DrawString(IO.File.ReadAllText("C:\Text.txt") , New Font("Microsoft Sans Serif", 8.25), Brushes.Yellow, 150, 15)
Loop
-
Jul 30th, 2009, 07:30 AM
#7
Re: Accessing text files
I'm pretty sure the error is on this line:
Code:
IO.File.WriteAllText("C:\Text.txt", IO.File.ReadAllText("C:\Text.txt") - 1)
When you call File.WriteAllText, it creates streamwriter internally to write to that file so the file is being locked. You then immediately do a File.ReadAllText on the same file that is being open for write.
What you need to do is read the file first, then wrtie to it.
Try this:
Code:
If Not IO.File.Exists("C:\Text.txt") Then
IO.File.WriteAllText("C:\Text.txt", 10)
Else
Dim currentValue As Integer = Integer.Parse(IO.File.ReadAllText("C:\Text.txt")
IO.File.WriteAllText("C:\Text.txt", (currentValue -1).ToString())
End If
End If
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 3rd, 2009, 08:21 AM
#8
Thread Starter
Member
Tags for this Thread
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
|