Working with file class in VB.NET
This article will explain about the File class and its operations.
Introduction
This article will explain about the File class and its operations. The System.IO is one of the significant names which are used to work with the file in .Net framework. Let us see about This class.
File Class
File class exposes many static methods for moving, copying, and deleting files. There are the static methods that involve moving a file, copying and deleting a file.
some of the most useful static methods of the file class are:
S.No |
Method |
Description |
1 |
Copy |
This method is used to copy a file to the specified location. |
2 |
Create |
This method is used to create a file in the specified path. |
3 |
Delete |
This method is used to Delete a file. |
4 |
Open |
This method is used to return a filetream object at the specified path. |
5 |
Move |
Moves a specified file to a new location. we can specify a different name for the file in the new location. |
6 |
Exists |
Determines whether the specified file exists. |
7 |
OpenRead |
Opens an existing file for reading. |
8 |
OpenWrite |
Opens an existing file or creates a new file for writing. |
Import two namespace.
Imports System.IO
Imports System.Text
Create Method:
This method is used to create a file in the specified path.
Sub Main()
Dim path As String = "D:\MyTestFile12.txt"
Dim fs As FileStream =File.Create(path)
If True Then
Dim info As [Byte]() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
fs.Write(info, 0, info.Length)
Console.WriteLine("File has been created")
End If
|
Delete Method
This method is used to Delete a file.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim path As String = "D:\MyTest1.txt"
File.Delete(path)
Console.WriteLine("File has been deleted")
End Sub
End Module
|
Open Method
This method is used to return a filetream object at the specified path.
Exists Method
Determines whether the specified file exists.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim path As String = "D:\MyTestFile1.txt"
Console.WriteLine(If(File.Exists(path),"File exists.","File does not exist."))
End Sub
End Module
|
Copy Method
This method is used to copy a file to the specified location.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim path As String = "D:\MyTestFile1.txt"
Dim path1 As String = "D:\MyTest1.txt"
File.Copy(path, path1)
Console.WriteLine("File has been copied")
End Sub
End Module
|
Move Method
Moves a specified file to a new location. we can specify a different name for the file in the new location.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim path As String = "D:\MyTestFile1.txt"
Dim path1 As String = "c:\MyTest1.txt"
File.Move(path, path1)
Console.WriteLine("File has been moved")
End Sub
End Module
|
OpenRead method
Opens an existing file for reading.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim path As String = "d:\MyTest1.txt"
If Not File.Exists(path) Then
' Create the file.
Dim fs As FileStream =File.Create(path)
If True Then
Dim info As [Byte]() = New UTF8Encoding(True).GetBytes("This is a file")
fs.Write(info, 0, info.Length)
End If
End If
Using fs As FileStream =File.OpenRead(path)
Dim b As Byte() = New Byte(1023) {}
Dim temp As New UTF8Encoding(True)
While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
End While
End Using
End Sub
End Module
|
Conclusion:
File class is very useful when we work with the files . If there is any mistake then please notify me. I expect your valuable comments and feedback about this article.