如何:在 Visual Basic 中确定文件的属性

更新:2007 年 11 月

My.Computer.FileSystem.GetFileInfo 方法可用于获取包含有关指定文件的信息的 FileInfo 对象,包括 FileAttributes 枚举。

此表显示了 FileAttributes 的成员。

成员

说明

Archive

此文件的存档状态。应用程序使用此属性将文件标记为备份或移除。

Compressed

此文件是压缩文件。

Device

目前不使用此成员。

Directory

此文件是一个目录。

Encrypted

文件中的所有数据均加密。

Hidden

此文件是隐藏的,因此将不会显示在普通目录列表中。

Normal

此文件未设置其他属性。

NotContentIndexed

将不会通过操作系统的内容索引服务来索引此文件。

Offline

此文件处于脱机状态,此刻不能使用其数据。

ReadOnly

此文件是只读的。

ReparsePoint

此文件包含一个重新分析点,该点是用户定义数据组成的块。

SparseFile

此文件是稀疏文件。稀疏文件通常是大文件,包含的数据大多数为零。

System

此文件是系统文件。它是操作系统的一部分,或者由操作系统以独占方式使用。

Temporary

此文件是临时文件。文件系统尝试将所有数据保存在内存中,而不是将数据刷新回大容量存储,以便可以快速访问。当临时文件不再需要时,应用程序应立即删除它。

确定文件是否已加密

  1. 获取要检查的文件的 FileInfo 对象。此示例获取文件 Testfile.txt 的 FileInfo 对象。

    Dim infoReader As System.IO.FileInfo
    infoReader = My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")
    
  2. FileInfo 对象获取 FileAttributes 对象。此示例从 FileInfo 对象获取 FileAttributes 对象。

    Dim attributeReader As System.IO.FileAttributes
    attributeReader = infoReader.Attributes
    
  3. 查询 FileAttributes。此示例确定文件是否已加密并显示相应的结果。

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

请参见

任务

如何:在 Visual Basic 中确定文件是否为隐藏文件

参考

FileAttributes

FileInfo

My.Computer.FileSystem 对象

My.Computer.FileSystem.GetFileInfo 方法

其他资源

文件、目录和驱动器属性 (Visual Basic)