Home »
VB.Net »
VB.Net Programs
VB.Net program to set the attributes of the specified file
By Nidhi Last Updated : November 16, 2024
Setting the attributes of a file in VB.Net
Here, we will set attributes of a specified file using the SetAttributes() method of the File class.
File attributes can be the following:
- Archive
- Compressed
- Device
- Hidden
- ReadOnly etc
Program/Source Code:
The source code to set the attributes of the specified file is given below. The given program is compiled and executed successfully.
VB.Net code to set the attributes of the specified file
'VB.Net program to set the attributes of a specified file.
Imports System.IO
Module Module1
Sub Main()
Try
Dim f1 As FileAttributes
f1 = File.GetAttributes("abc.txt")
Console.WriteLine("Attributes before Method Call are :" + f1.ToString())
File.SetAttributes("abc.txt", FileAttributes.ReadOnly)
Dim f2 As FileAttributes
f2 = File.GetAttributes("abc.txt")
Console.WriteLine("Attributes After Method Call are :" + f2.ToString())
Catch ex As FileNotFoundException
Console.WriteLine("File does not exist")
End Try
End Sub
End Module
Output
Attributes before Method Call are :Archive
Attributes After Method Call are :ReadOnly
Press any key to continue . . .
Explanation
In the above program, we created the module Module1 that contains the Main() function. The Main() function is the entry point for the program. Here, we set the ReadOnly attribute of the "sample.txt" file, and then we printed the attributes of the file on the console screen.
VB.Net File Handling Programs »