更新:2007 年 11 月
返回表示文件、目录或文件夹的属性的 FileAttribute 值。
在文件 I/O 操作中,My 功能具有比 FileAttribute 更高的效率和更好的性能。有关更多信息,请参见 My.Computer.FileSystem 对象。
Public Function GetAttr(ByVal PathName As String) As FileAttribute
参数
- PathName
 必选。指定文件、目录或文件夹名的 String 表达式。PathName 可以包括目录(或文件夹)和驱动器。
返回值
由 GetAttr 返回的值是下列枚举值的和:
| 值 | 常数 | 说明 | 
|---|---|---|
| Normal | vbNormal | 正常。 | 
| ReadOnly | vbReadOnly | 只读。 | 
| Hidden | vbHidden | 隐藏。 | 
| System | vbSystem | 系统文件。 | 
| Directory | vbDirectory | 目录或文件夹。 | 
| Archive | vbArchive | 自从上次备份后文件已更改。 | 
| Alias | vbAlias | 文件具有不同的名称。 | 
| .gif) 说明: | 
|---|
| 这些枚举是由 Visual Basic 语言指定的。可以在代码中的任何位置使用这些名称以代替实际值。 | 
异常
| 异常类型 | 错误号 | 条件 | 
|---|---|---|
| Pathname 无效或包含通配符。 | ||
| 目标文件不存在。 | 
如果正在升级使用非结构化错误处理方式的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象) 比较错误号。) 然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述 替换这种错误控制。
备注
若要确定设置了哪几个属性,请使用 And 运算符对 GetAttr 函数返回的值以及您想要的个别文件属性值执行按位比较。如果结果不是零,则为命名的文件设置该属性。例如,如果没有设置 Archive 属性,则下列 And 表达式的返回值为零:
Result = GetAttr(FName) And vbArchive
如果设置了 Archive 属性,则返回非零值。
示例
此示例使用 GetAttr 函数确定文件和目录或文件夹的属性。
Dim MyAttr As FileAttribute
' Assume file TESTFILE is normal and readonly.
MyAttr = GetAttr("C:\TESTFILE.txt")   ' Returns vbNormal.
' Test for normal.
If (MyAttr And FileAttribute.Normal) = FileAttribute.Normal Then
   MsgBox("This file is normal.")
End If
' Test for normal and readonly.
Dim normalReadonly As FileAttribute
normalReadonly = FileAttribute.Normal Or FileAttribute.ReadOnly
If (MyAttr And normalReadonly) = normalReadonly Then
   MsgBox("This file is normal and readonly.")
End If
' Assume MYDIR is a directory or folder.
MyAttr = GetAttr("C:\MYDIR")
If (MyAttr And FileAttribute.Directory) = FileAttribute.Directory Then
   MsgBox("MYDIR is a directory")
End If
智能设备开发人员说明
不支持此函数。
要求
**模块:**FileSystem
**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)