Text File Operations VB.NET

free web hosting
Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > .NET (VB, C# & J#)

Text File Operations VB.NET

bob3695
In this tutorial you will learn most of the operations you can use on a

text file. They include finding if a file exists, opening/creating a

file, reading/writing file, closing file, copying file, deleting file.

You will need a form with two buttons on it. Use the names Step1 and

Step2.

First thing we are going to do is import system.IO. To do this go into

the forms code view. At the very top add this line.

CODE

Imports system.IO


This lets us use the file operations that we need for this tutorial.

Next we need to check to see if a file exists. To do this double click

on the step1 button and type this code.

CODE

If File.Exists("TextFile.txt") then


This line just checks if the file already exists. Next type this line

of code.

CODE

Dim ioFile as new StreamReader("TextFile.txt")


This line of code opens "TextFile.txt" for reading. Now we are going to

read the file and store it into a variable.

CODE

Dim ioLine as string ' Going to hold one line at a time
Dim ioLines as string ' Going to hold whole file
ioLine = ioFile.ReadLine
ioLines = ioLine
While not ioLine = ""
ioLine = ioFile.ReadLine
ioLines = ioLines & vbcrlf & ioLine
End While


We now have the whole text file read into ioLines. Lets display the

text in a message box.

CODE

MsgBox(ioLines)


Now we are going to close the file.

CODE

ioFile.close


Now that we are done reading a file. Lets do the else statement of the

if File.Exists statement. The way this code is going to be set-up is if

the file exists the program reads it. If it doesn't exist it writes a

file.

CODE

Else
Dim ioFile as new StreamWriter("TextFile.txt")


We now have a file open for writting. Lets write two lines to it.

CODE

ioFile.WriteLine("Hi There")
ioFile.WriteLine("User")


So we have some text in the file now. Lets close the file.

CODE

ioFile.Close


We are done with reading/writting files so lets end the if statement

CODE

End If


That is it for that button. Now lets move onto deleteing files. Go back

to form view and double click on the second button. In the code add

these lines.

CODE

If File.Exists("TextFile.txt")
File.Delete("TextFile.txt")
else
msgbox("File doesn't exist")
end if


All this code does is check for the file then if it exists deletes it.

If it doesn't exist then it tells the user that it doesn't exist.

Thats it for this tutorial.

Thanks,
Rich

 

 

 


Reply

h3lium
another valuable concept to learn to work with text file is the stream class. It is especially useful for reading files from over the internet.

Reply

coolblinger
You can use:
CODE
Dim path As String
        Dim returnValue As String

        path = "1.txt"

        returnValue = TextBox1.Text

        File.WriteAllText(path, returnValue)

And write:
CODE
Dim path As String
        Dim returnValue As String

        path = "1.txt"

        returnValue = File.ReadAllText(path)

        TextBox1.Text = returnValue

Reply

iGuest
multi lines in text file
Text File Operations VB.NET

I can write one line in a text file how I can write multi lines in a text file using vb.Net

-question by arif

Reply

iGuest
how to add a new line to the end of an existing file
Text File Operations VB.NET

How to add a new line to the end of an existing file

-reply by rokesh

Reply

iGuest
How can i search string or text in file content
Text File Operations VB.NET

I need to build application similar to windows desktop search. I need to search text or string in file name and inside file content. And stop the search through stop button.



Can you help me with this?



-reply by Beginner

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. vb.net open text file and display in list - 0.26 hr back. (1)
  2. vb.net show text - 0.35 hr back. (1)
  3. deleteline vb.net - 0.36 hr back. (1)
  4. open txt file vb 2008 - 0.40 hr back. (1)
  5. vb.net 2003 read file - 1.37 hr back. (1)
  6. vb 2008 read from text file - 1.87 hr back. (1)
  7. vb.net deleting a file if it exists - 2.07 hr back. (1)
  8. create text file vb/net - 2.16 hr back. (1)
  9. file operations in vb.net - 2.32 hr back. (1)
  10. vb.net write line to text file - 2.36 hr back. (1)
  11. vb.net find string in text file - 2.65 hr back. (1)
  12. "visual basic .net" "file operations" - 3.11 hr back. (1)
  13. vb and file open - 3.28 hr back. (1)
  14. how to close a file in vb.net - 3.34 hr back. (1)
Similar Topics

Keywords : text, file, operations, vb, net

  1. Performing Dos Operations From Visual Basic
    The easiest and most powerful way to do (1)
  2. VB.NET: Rotating Label & Angled Text Control
    Write text at any angle with this (8)
    Language: Visual Basic.NET 2003 Description: Standard Windows Form Label control capable of
    rotating its text from -360 to 360 degrees. This control has two reference modes, center and
    quadrant. Sample: In a recent programming endeavor I had the need to have text displayed in the
    vertical position. As usual, what I thought would be a 10 min solution became a 3 day journey. I
    just assumed that the standard windows form label control had a build in property that would
    determine the orientation. Once I looked and Googled for the property I soon found I was out of lu....

    1. Looking for text, file, operations, vb, net

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for text, file, operations, vb, net
advertisement




Text File Operations VB.NET



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE