Partager via


GetAttr Function 

Returns a FileAttribute value representing the attributes of a file, directory, or folder.

The My feature gives you greater productivity and performance in file I/O operations than FileAttribute. For more information, see My.Computer.FileSystem Object.

Public Function GetAttr(ByVal PathName As String) As FileAttribute

Parameters

  • PathName
    Required. String expression that specifies a file, directory, or folder name. PathName can include the directory or folder, and the drive.

Return Value

The value returned by GetAttr is the sum of the following enumeration values:

Value Constant Description

Normal

vbNormal

Normal.

ReadOnly

vbReadOnly

Read-only.

Hidden

vbHidden

Hidden.

System

vbSystem

System file.

Directory

vbDirectory

Directory or folder.

Archive

vbArchive

File has changed since last backup.

Alias

vbAlias

File has a different name.

Note

These enumerations are specified by the Visual Basic language. The names can be used anywhere in your code in place of the actual values.

Exceptions

Exception type Error number Condition

IOException

52

Pathname is invalid or contains wildcards.

FileNotFoundException

53

Target file does not exist.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

To determine which attributes are set, use the And operator to perform a bitwise comparison of the value returned by the GetAttr function and the value of the individual file attribute you want. If the result is not zero, that attribute is set for the named file. For example, the return value of the following And expression is zero if the Archive attribute is not set:

Result = GetAttr(FName) And vbArchive

A nonzero value is returned if the Archive attribute is set.

Example

This example uses the GetAttr function to determine the attributes of a file and directory or folder.

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

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

And Operator (Visual Basic)
FileAttr Function
SetAttr Function
IOException Class
FileNotFoundException Class
FileAttribute Enumeration

Other Resources

File, Directory, and Drive Properties in Visual Basic