bookmark - Text File Operations VB.NET

Text File Operations VB.NET

 
 Discussion by bob3695 with 19 Replies.
 Last Update: August 10, 2011, 6:47 am (View Latest)
Page 1 of 2 pages.
bookmark - Text File Operations VB.NET  
    
free web hosting
 
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

Sun Jun 26, 2005    Reply    New Discussion   


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.

Wed Aug 10, 2005    Reply    New Discussion   

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

Mon Mar 17, 2008    Reply    New Discussion   

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

Wed Apr 23, 2008    Reply    New Discussion   


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

Tue Apr 29, 2008    Reply    New Discussion   

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

Sat Jun 14, 2008    Reply    New Discussion   

I am very thankful to the organization for such a place where we can really learn a lot individually online

Regards


-feedback by mahendra singh b

Thu Nov 20, 2008    Reply    New Discussion   

ControlsText File Operations VB.NET

I have taken form1 and label11 on it.

now I have created a class "label". In this class I want to write " label1.Text="hello" " and when I debug, on my form it should appear as " hello"

please help in code

-question by bhushan

Sun Jul 19, 2009    Reply    New Discussion   

Read text fileText File Operations VB.NET

Hi !

I work for telecommunication company and we have subriber records in a text file and it's look like this 

<BEGINFILE>

<SUBBEGIN

MODE=ADDSUB;

IMSI=637820132901781;

MSISDN=25265370781;

CAT=COMMON;

CURRENTNAM=NONGPRS;

SMDP=MSC;

KI=7fa4dcb25e4a676c8d329c4c8ee073cd;

CARDTYPE=SIM;

K4SNO=1;

ALG=2;

KI_USEDFLAG=YES;

TBS=TS11&TS12&TS21&TS22&TS61&TS62&BS21&BS22&BS23&BS24&BS25&BS26&BS31&BS32&BS33&BS34&BS41&BS42&BS43&BS44&BS45&BS46&BS51&BS52&BS53&BS61&BS62&BS81&BS82;

CW=CW-ALL-PROV;

GS=CLIP&HOLD;

CONTROL=SUB;

PW=0000;

WPA=0;

CLIPOC=NO;

VLR=2526500004;

MSC=2526500004;

DEFCALL=TS11;

<SUBEND

<ENDFILE>

 

 I need to read those line between <SUBBEGIN and <SUBEND

please help how can do that 

I'm using Visua basic 2008 VB.Code

Thank you

-question by liban A. Ali

Wed Sep 2, 2009    Reply    New Discussion   

What if you have a blank line in the file?Text File Operations VB.NETReplying to coolblingerThe code you posted will not read a file containing a blank line. I re-wrote as follows and was able to handle files with blank lines.Dim ioOutput as StringIf File.Exists("testfile.Txt") Then Dim ioFile As New StreamReader("testfile.Txt") Dim ioLine As String = "" Dim ioLines As String = "" While Not ioFile.EndOfStream ioLine = ioFile.ReadLine ioLines &= ioLine End While ioOutput = ioLines ioFile.Close() End If 

Fri Aug 14, 2009    Reply    New Discussion   

VB 2005Text File Operations VB.NET

Hi,

I'm a student and I want to know how to design a program that reads text files (*.Txt) without using a database, and the program should be able to retrieve files that contain documents...

-reply by Sewe Oyaya

 


Mon Oct 12, 2009    Reply    New Discussion   

vb codeText File Operations VB.NETDear sir/madamCan you please assist me with a visual basic code that will enable me to extract data from a text file and display it in a tabular form in a visual basic window.I wont to be displaying names, date and location.It will be of much help to me and a pleasure as well.Thank you in advance-
 
question by stanford bonongwe

Sat Dec 12, 2009    Reply    New Discussion   

vb codeText File Operations VB.NET

 dear/sir

Am stanford bonongwe and am a student who is leaning visual basic programing.

I have a problem in extracting data from a text file and displaying it in a tabuler form on a visual basic window. Can you please assist me with acode and the procedures that I need to do in order to come up with the above mentioned problem.

thank you for you help in advance

yours faithfully 

stanford Bonongwe

-question by stanford bonongwe

 


Sat Dec 12, 2009    Reply    New Discussion   

vb codeText File Operations VB.NET

Dear sir/madamCan you please assist me with a visual basic code that will enable me to extract data from a text file and display it in a tabular form in a visual basic window.I wont to be displaying names, date and location.It will be of much help to me and a pleasure as well.Thank you in advance

-question by stanford bonongwe


Sat Dec 12, 2009    Reply    New Discussion   

Extracting data from text file into seperate CSV fileText File Operations VB.NET

I have a file similar to the below

blah blah blah label="ABC" label.Value="123" label.Value="456" label.Value="789" label="DEF" label="HIJ" label.Value="111" label.Value="222" blah blah blah. (No CrLf, just  along string)

What I want to do is to be able to extract the values of label and label.Value into a excel sheet. So ideally the file above would end up in excel like...                          

label ABC

label.Value 123

label.Value 456

label.Value 789

label DEF

label HIJ

label.Value 111

label.Value 222

where label and label.Value are in column A and the value in qutoes is in column B (minus the actual quotes)

Does anyone have any examples of VBA code that I can use to do this?

-question by Manish Patel

Mon Feb 8, 2010    Reply    New Discussion   

Quickly Post to Text File Operations VB.NET  w/o signup Share Info about Text File Operations VB.NET  using Facebook, Twitter etc. email your friend about Text File Operations VB.NET Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print


How To: Render Loop C#  How To: Render Loop C# (0) (13) VB.NET: Rotating Label & Angled Text Control Write text at any angle with this  VB.NET: Rotating Label & Angled Text Control Write text at any angle with this