C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » VB.NET Syntax »

StreamReader and StreamWriter class


Posted Date: 21 Oct 2009    Resource Type: Code Snippets    Category: VB.NET Syntax
Author: NeetuMember Level: Diamond    
Rating: 1 out of 5Points: 12



Dealing with File Handling with the help of StreamReader and StreamWriter class



Imports System
'System.IO namespace is used to deal with the file input output hanlding.
Imports System.IO
'Imports System.Text

Module Module3

Public Class textEx
Public Shared Sub main()
'Path is a variable that store path of file.
Dim path As String = "c:\test.txt"
'File is a class that contain Exists method and it takes argument as path and return true or false.
'If file exists then return true else return false.
If File.Exists(path) = False Then
'StreamWriter is a class that is used to write the text in the file.


Dim sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("Hello How are you")
'Flush method is used to clear all the buffer for the current writer.
sw.Flush()
'Closes the streamWriter object
sw.Close()
Else
System.Console.WriteLine("File Already Present")

End If
Try
'StreamReader is used to read the data from file.
'File.OpenText(Path_of_file) is used to open file in reading mode.
Dim sr As StreamReader = File.OpenText(path)
'Peek method returns the next character in the stream and return negative values when reaches to end of file.
Do While sr.Peek >= 0
Console.WriteLine(sr.ReadLine)
Loop
sr.Close()
Dim path2 As String = "c:\one.txt"
'File.Delete(File_Path) is used to delete particular file.
File.Delete(path2)
'File.Copy(Source,Destination) is used to copy the file.
File.Copy(path, path2)
Console.WriteLine("{0} was copied to {1}", path, path2)


Catch ex As Exception
Console.WriteLine("The Process Fail :{0}", ex.ToString())

End Try
End Sub
End Class
End Module


regards
Neetu



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
File Handling in vb.net  .  

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: Download File using VB code
Previous Resource: Three Tier application
Return to Discussion Resource Index
Post New Resource
Category: VB.NET Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use