C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






How to read the contents of a text file


Posted Date: 29 May 2008    Resource Type: Code Snippets    Category: File Operations
Author: RekhaMember Level: Gold    
Rating: Points: 10



The following sample code shows how to read the contents of a text file

Private Sub btnReadTextFile_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles btnReadTextFile.Click
Dim winPath As String
Dim s As String
Dim fname As String
Dim sr As StreamReader
Dim ex As Exception
' get the windows folder name
winPath = System.Environment. _
GetEnvironmentVariable("c:")
If winPath = "" Then
MessageBox.Show("Unable to retrieve the " & _
"Windows path.", "Error in Reading Windows Path", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
fname = winPath & "\win.txt"
If File.Exists(fname) Then
Try
sr = File.OpenText(fname)
s = sr.ReadToEnd
sr.Close()
textbox1.text = s
Catch ex
MessageBox.Show(ex.message)
return
End Try
Else
MessageBox.Show("The file " & fname & _
" does not exist.")
Return
End If
End Sub




Responses

Author: Kapil Dhawan    18 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil




Author: Gaurav Arora    03 Aug 2008Member Level: Gold   Points : 1
Hi!

This one is nice; it’s a tiny but with worth of meaning. I appreciate your way of sharing knowledge.



Author: RamyaNaidu    06 Aug 2008Member Level: Silver   Points : 2
Dim writer As StreamWriter = File.CreateText("D:\myfile.txt")
writer.WriteLine("Out to file.")
writer.Close()
Dim reader As StreamReader = File.OpenText("D:\myfile.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = reader.ReadLine()
End While
reader.Close()


Author: Raghav    17 Aug 2008Member Level: Gold   Points : 1
Nice Piece of code. Hope you can provide some more good code example for the benefits of the members

Raghav
Webmaster


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Javascript code for Retrieving file name from fileupload control
Previous Resource: Renaming the text file
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use