How to check a file is system file

The below code check a file is system file or not. To do that we use GetAttributes method of the file class. The System.IO namespace is one of the most significant namespaces used for working with file path in the .Net Framework.

GetAttributes Method - This method is used to Get the FileAttributes of the file on the path.

Example

Imports System.Text

Imports System.IO

 

Module Module1

    Sub Main()

        Dim str As String = "C:\WINDOWS/bootstat.dat"

        Dim isSystem As Boolean = (File.GetAttributes(str) And FileAttributes.System)

        If (isSystem = False) Then

            Console.WriteLine(" This is not a system file")

        Else

            Console.WriteLine("This is a system file")

        End If

    End Sub

End Module

Hide a file

This example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

SetAttributes - Sets the FileAttributes of the file on the specified path.

Imports System.Text

Imports System.IO

 

Module Module1

    Sub Main()

        Dim str As String = "d:\Test.dat"

        File.SetAttributes(Str, File.GetAttributes(Str) Or FileAttributes.Hidden)

        Console.WriteLine("The {0} file is now hidden.", Str)

    End Sub

End Module