Results 1 to 6 of 6

Thread: run time error for reading the text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    run time error for reading the text file

    Hi

    I am running the below mention code in vb.net but problem is
    when dir.txt already in c: drive it will read the text file
    but if i delete the dir.txt file and create new one and in next line
    try to read the same dir.txt file it will flash error "Could not find file c:\dir.txt"

    Pls guide me .. why this type of problem accured..


    thanks


    asm

    VB Code:
    1. Imports System
    2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim SFile As System.IO.File
    4.         Dim VolRead As System.IO.StreamReader
    5.         Dim ReadTxt As String
    6.         Try
    7.             If SFile.Exists("C:\DIR.TXT") Then
    8.                 System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
    9.             End If
    10.             System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Dir C:  > C:\DIR.TXT")
    11.             VolRead = New System.IO.StreamReader("C:\DIR.TXT")
    12.             ReadTxt = VolRead.ReadToEnd()
    13.             Label1.Text = "READING TEXT FILE OK.."
    14.             VolRead.Close()
    15.             If SFile.Exists("C:\Dir.txt") Then
    16.                 System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
    17.             End If
    18.         Catch ex As Exception
    19.             Label1.Text = ex.Message
    20.         End Try
    21.     End Sub

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: run time error for reading the text file

    Hi, i don't have an answer to your problem but you shouldn't repeat a thread!

    Good luck!
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    Re: run time error for reading the text file

    The problem is you delete the file, then you try to read it right after that
    VB Code:
    1. If SFile.Exists("C:\DIR.TXT") Then
    2. 'OK, the file exists... You start cmd.exe and delete the file right here in the line below
    3.                 System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C [B][Color=Red]Del C:\DIR.TXT"[/Color][/B])
    4.             End If
    5.             'The file got deleted as the result of the above code... Now you try to open it... Why?
    6.             System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Dir C:  > C:\DIR.TXT")

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: run time error for reading the text file

    stanav, that second line isn't trying to read the file. It's performing an operation and redirecting the output to that file.

    So, are you saying that this is the line that fails?
    VB Code:
    1. VolRead = New System.IO.StreamReader("C:\DIR.TXT")
    If so it is because the file hasn't been recreated yet when you try to create the StreamReader. Process.Start doesn't wait for the process to complete, so your process is still running when you try to read the file. You should wait for the process to complete before trying to read the file. Also, you shouldn't be hard-coding the path of cmd.exe because there's no guarantee that Windows is installed on the C drive. The system32 folder is in the environment path variable so you don't need to specify the path at all.
    VB Code:
    1. System.Diagnostics.Process.Start("cmd", " /C Dir C:  > C:\DIR.TXT")[U].WaitForExit()[/U]
    2.             VolRead = New System.IO.StreamReader("C:\DIR.TXT")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: run time error for reading the text file

    Quote Originally Posted by jmcilhinney
    stanav, that second line isn't trying to read the file. It's performing an operation and redirecting the output to that file.
    Ops... my dumd mistake. Thank you for pointing it out.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: run time error for reading the text file


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