How to: Determine a File's Attributes in Visual Basic

The GetFileInfo method can be used to get a FileInfo object, which contains information about the specified file, including a FileAttributes enumeration.

This table shows the members of FileAttributes.

Member

Description

Archive

The file's archive status. Applications use this attribute to mark files for backup or removal.

Compressed

The file is compressed.

Device

This member is not used at this time.

Directory

The file is a directory.

Encrypted

All of the data in the file is encrypted.

Hidden

The file is hidden and will not be displayed in an ordinary directory listing.

Normal

The file has no other attributes set.

NotContentIndexed

The file will not be indexed by the operating system's content indexing service.

Offline

The file is offline. The data in the file is not immediately available.

ReadOnly

The file is read-only.

ReparsePoint

The file contains a reparse point, which is a block of user-defined data.

SparseFile

The file is a sparse file. Sparse files are usually large files containing data that is mostly zeroes.

System

The file is a system file. The file is part of the operating system or is used exclusively by the operating system.

Temporary

The file is temporary. File systems attempt to keep all of the data in memory for quicker access, rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.

To determine if a file is encrypted

  1. Get a FileInfo object for the file you wish to examine. This example gets a FileInfo object for the file Testfile.txt.

    Dim infoReader As System.IO.FileInfo
    infoReader = My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")
    
  2. Get a FileAttributes object from the FileInfo object. This example gets FileAttributes from the FileInfo object.

    Dim attributeReader As System.IO.FileAttributes
    attributeReader = infoReader.Attributes
    
  3. Query FileAttributes. This example determines if the file is encrypted and displays a result accordingly.

    If (attributeReader And System.IO.FileAttributes.Encrypted) > 0 Then
        MsgBox("File is encrypted!")
    Else
        MsgBox("File is not encrypted!")
    End If
    

See Also

Tasks

How to: Determine if a File is Hidden in Visual Basic

Reference

FileAttributes

FileInfo

FileSystem

GetFileInfo

Other Resources

File, Directory, and Drive Properties in Visual Basic