Thursday, May 26, 2016

Write in exist text files vb.net






Write in exist text files VB.net
Today I will tch you how to write in text files (.txt) using VB.Net. Let´sgo :D
How to crte a project in Visual Studio?
Exists many ways to crte a new project in VS. For example, when you open your VS program, in theStart Pageyou will see the optionNewProject ...If you can´t see the Start Page you can also click inFile > New Project > Select Visual Basic > Forms AppliionRename your project name if you want and clickOK.It´s sy!
Start in the Form
In the form, add aTextBox(orrichtextbox) to place the text from the file. The textbox should supportMultiLine. You can enable it in the Properties of the textbox or using like I will show in the example. Center it in the form. Also we need a Button.
The …
PressControl + Alt + 0to open the page.Click incomboboxthat is in the top, left side. SelectFormX(events)and in theComboboxin the top right side selectLoad. This will erate theLoad Event of the FormWrite the next lines of :
PrivateSubForm3_Load(senderAsObject, eAsEventArgs)HandlesMe.LoadButton1.Text ="Save”TextBox1.Multiline =True
EndSub
Now do the same to the button. Select in the leftButton1and in the right sideClickor more simple do double-click in the button in the designer.Write this:
PrivateSubButton1_Click(senderAsObject, eAsEventArgs)HandlesButton1.ClickIfSystem.IO.File.Exists("C:\Test\About.inf")Then'check if file exists' 'If yes the write in fileTryDimobjwriterAsNewSystem.IO.StrmWriter("C:\Test\About.inf")objwriter.Write(TextBox1.Text)objwriter.Close()MsgBox("File saved",vbInformation+vbOKOnly)chexAsExceptionMsgBox(ex.Message,vbCritical+vbOKOnly,"Error")EndTryEndIfEndSub

TheSystem.IO.StrmWriter, does not work only in.txt files, but also in other tipes like.inf or.ini and others. If the file is supported by the Notepad of it will also open if you use my this method. Is important use theTry ...ch ... End Trybecause if the file that you want to rd is being used by other process you app will crash. And also, if theSystem.IO.StrmWriterdoesn´t support that file.

No comments:

Post a Comment