File Handling in VB.Net

File handling is one of the important skills that a programmer should master because in file handling you can manipulate and manage file that are stored in a directory or even access and manipulate an operating system file. In this tutorial and VB.Net code below show an example on how to create a new file (text file) and it also show an example on how to open a file.

As a programmer you should have always a dedication to learn and to be innovative. File handling is also useful in other application development like mobile programming. The trend today is more users are using mobile devices like iPhone, smart phones and because of that programmers are also versatile so that they can always provide mobile application that can be use by the consumers or customers.

Learning on how to create program or application that can handle or manipulate a file is a good training to become a better and skillful programmer or application developer. In learning on how to create a better application or program it is important that a programmer understand how the process work manually. It is difficult to automate or computerize a system that in first place you do not know how that system operates manually. If a programmer or a developer has a better understanding on how the system are done manually it is easy to automate or create a computerize application or program for that manual system.

For example to create a program in VB.Net that will check if a certain file exist in a directory or folder and open it, the programmer should understand first on how to manually open a file that is stored in a directory. Then create an algorithm or solution on how to do that, convert it to code example using VB.Net programming language.

Below is the example code in VB.Net that will create a text file and write a text.

Public Class Form1

Imports System.IO

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click         
Dim path As String = String.Empty         
path = "D:\\test_" & DateTime.Now.ToString("ddMMyyyy_HHmmss") & ".txt"  
Dim txt As String = "The quick brown fox"         
Dim sw As StreamWriter         
sw = File.CreateText(path)         
sw.WriteLine(txt)         
sw.Close()         
MsgBox("Create new file and write Successfully!! Path is " & path)     
End Sub
End Class