There are many ways to write text or string to a file. In this tutorial, we are going to use StreamWriter
class and StringBuilder
class.
StreamWriter
is designed for character output in a particular encoding, whereas classes derived from Stream
are designed for byte input and output.
StringBuilder
class represents a string-like object whose value is a mutable sequence of characters.
Required Preferences
- System.Runtime.Extensions.dll for
StreamWriter
class - System.Runtime.dll for
StringBuilder
class
Procedure
- Open Visual Studio (Visual Studio 2019)
- Create new Windows Forms App (.NET Framework) for Visual Basic then click next
- Set the project name to WriteTextToFileVB, the location where to save the project, the solution name will automatically set but this can be set to any name example "Tutorials", set the framework (.NET Framework 4.7.2) or framework 1.1 or higher then click create
- The new project will be display in few seconds
- Rename the default form from Form1 to FormMain by renaming this at the project explorer or properties window at the right side then add 1 button control and 1 textbox control by click and dragging from the toolbox window on the left side to the FormMain
- Arrange the button and textbox controls accordingly. Set FormMain.Text property to "Text To File Writer" and set FormMain.StartPosition to "CenterScreen". Set the button Name property to "ButtonWrite" and Text property to "Write To File". Then, set the textbox Name property to "TextContainer", Multiline property to "True" and set the textbox ScrollBars property to "Vertical"
- Double click the ButtonWrite control to view and add code in the click event of the button control. The source code is available at the end of the page
- Then run the program
- Save the this project for the future tutorials
- That's all. I hope that you enjoy my tutorials. Subscribe to this page for more.
Code
1 | Public Class FormMain |
Download WriteTextToFileVB Project
Comments
Post a Comment