ARTICLE

Text file operations using VB.Net

Posted by Hirendra Sisodiya Articles | Visual Basic 2010 November 05, 2009
This article shows various text file operation with VB.Net.
 
Reader Level:

In this article we will learn different text file operations such as to know file exists or not, opening and creating a file, reading and writing file, copying and deleting file.

Dim
TempFile As System.IO.File
Dim TempRead As System.IO.StreamReader
Dim TempWrite As System.IO.StreamWriter

Info about file existence


If
File.Exists("C:\TempText.txt") Then
    MessageBox.Show("File found")
Else
    MessageBox.Show("File not found")
End If

Create file

TempWrite = File.CreateText(
"C:\TempText.txt")

Open file


TempRead = File.OpenText(
"C:\TempText.txt")

Read file

You can also read an entire text file from the current position to the end of the file by using the ReadToEnd method, as shown in the following code :

Dim
FullFileStr As String
TempRead = File.OpenText("C:\TempText.txt")
FullFileStr = TempRead.ReadToEnd()

Write file

TempWrite.WriteLine(
"Hello")

Copy file

Dim
fileDestinationStr As String = "D:\TempText.txt"
If File.Exists("C:\TempText.txt") Then
    File.Copy("C:\TempText.txt", fileDestinationStr)
End If

Delete file

If
File.Exists("C:\TempText.txt") Then
    File.Delete("C:\TempText.txt")
Else
    MessageBox.Show("File not found")
End If

Login to add your contents and source code to this article
share this article :
post comment
 

sorry
i am not understanding your problem....please explain
thanks

Posted by Hirendra Sisodiya Jun 21, 2010

Im now need to play with text box in vb.net...
the basic function is,when the text box is fill by string(using barcode scanner),
it will do some validation.
so i put textBox1.text="" in if else command..
but the problem is...let say my string is"eating"..
but it only fill the "e"...
help me..
how i need to play with if else and textbox function...

regards.

Posted by abdul shafiq abdul rahman May 31, 2010

Thanks for your feedback.
It is highly apreciated.

Posted by abdul shafiq abdul rahman May 06, 2010

See Write file section in the article....

thanks

Posted by Hirendra Sisodiya May 06, 2010

Thanks Hirendra.
It works.
Based on your past experiance, can me add the existing text pad to add more data?

Thanks and Regards.

Posted by abdul shafiq abdul rahman May 06, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor