Option ExplicitPrivate Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As LongConst INVALID_HANDLE_VALUE = -1 Const FILE_ATTRIBUTE_DIRECTORY = &H10 Public Function Test(ByVal FilePath As String) Dim lRet As Long
lRet = GetFileAttributes(FilePath)
If lRet = INVALID_HANDLE_VALUE Then MsgBox "Error" ElseIf lRet And FILE_ATTRIBUTE_DIRECTORY Then MsgBox "Fold" Else MsgBox "File" End If
End Function============================= Option ExplicitPrivate Sub Dir1_Click() Test Dir1.path End SubPrivate Sub File1_Click() Test File1.FileName End Sub
在c:新建文件夹ttt.ttt和文件tt.tt Private Sub Command1_Click() CheckAttr "c:\ttt.ttt" End SubPrivate Sub Command2_Click() CheckAttr "c:\tt.tt" End Sub Public Function CheckAttr(strName As String) If Dir(strName, vbNormal) <> "" Then MsgBox strName & " 是文件" ElseIf Dir(strName, vbDirectory) <> "" Then MsgBox strName & " 是文件夹" End If End Function
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Function Test(ByVal FilePath As String) Dim lRet As Long
lRet = GetFileAttributes(FilePath)
If lRet = INVALID_HANDLE_VALUE Then
MsgBox "Error"
ElseIf lRet And FILE_ATTRIBUTE_DIRECTORY Then
MsgBox "Fold"
Else
MsgBox "File"
End If
End Function=============================
Option ExplicitPrivate Sub Dir1_Click()
Test Dir1.path
End SubPrivate Sub File1_Click()
Test File1.FileName
End Sub
Private Sub Command1_Click()
CheckAttr "c:\ttt.ttt"
End SubPrivate Sub Command2_Click()
CheckAttr "c:\tt.tt"
End Sub
Public Function CheckAttr(strName As String)
If Dir(strName, vbNormal) <> "" Then
MsgBox strName & " 是文件"
ElseIf Dir(strName, vbDirectory) <> "" Then
MsgBox strName & " 是文件夹"
End If
End Function