Results 1 to 8 of 8

Thread: [RESOLVED] Accessing text files

  1. #1

    Thread Starter
    Member Giver's Avatar
    Join Date
    Aug 2008
    Posts
    39

    Resolved [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.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  3. #3
    Addicted Member
    Join Date
    Jul 2009
    Posts
    138

    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?

  4. #4

    Thread Starter
    Member Giver's Avatar
    Join Date
    Aug 2008
    Posts
    39

    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.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  6. #6

    Thread Starter
    Member Giver's Avatar
    Join Date
    Aug 2008
    Posts
    39

    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

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  8. #8

    Thread Starter
    Member Giver's Avatar
    Join Date
    Aug 2008
    Posts
    39

    Re: Accessing text files

    thanks, it works!

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
  •  



Click Here to Expand Forum to Full Width