|
-
Dec 5th, 2006, 06:00 AM
#1
Thread Starter
Hyperactive Member
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:
Imports System
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim SFile As System.IO.File
Dim VolRead As System.IO.StreamReader
Dim ReadTxt As String
Try
If SFile.Exists("C:\DIR.TXT") Then
System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
End If
System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Dir C: > C:\DIR.TXT")
VolRead = New System.IO.StreamReader("C:\DIR.TXT")
ReadTxt = VolRead.ReadToEnd()
Label1.Text = "READING TEXT FILE OK.."
VolRead.Close()
If SFile.Exists("C:\Dir.txt") Then
System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
End If
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
-
Dec 5th, 2006, 06:22 AM
#2
Fanatic Member
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 
-
Dec 5th, 2006, 08:04 AM
#3
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:
If SFile.Exists("C:\DIR.TXT") Then
'OK, the file exists... You start cmd.exe and delete the file right here in the line below
System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C [B][Color=Red]Del C:\DIR.TXT"[/Color][/B])
End If
'The file got deleted as the result of the above code... Now you try to open it... Why?
System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Dir C: > C:\DIR.TXT")
-
Dec 5th, 2006, 08:48 AM
#4
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:
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:
System.Diagnostics.Process.Start("cmd", " /C Dir C: > C:\DIR.TXT")[U].WaitForExit()[/U]
VolRead = New System.IO.StreamReader("C:\DIR.TXT")
-
Dec 5th, 2006, 10:02 AM
#5
Re: run time error for reading the text file
 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.
-
Dec 5th, 2006, 10:20 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|