VB获取和更改文件属性

VB获取和更改文件属性

摘自网络供自己备查!

Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100

修改:
Private Sub Command1_Click()
SetFileAttributes "C:\1.txt", 1
End Sub

Private Sub Command2_Click()
SetFileAttributes "C:\1.txt", 2
End Sub

判断:

Private Sub Command1_Click()
Print GetAttr("C:\Autoexec.Bat")
If GetFileAttributes("C:\1.Bat") And 1 Then Print "此文件只读"
If GetFileAttributes("C:\1.Bat") And 2 Then Print "此文件隐藏"
If GetFileAttributes("C:\1.Bat") And 4 Then Print "此文件为系统文件"
If GetFileAttributes("C:\1.Bat") And 16 Then Print "此文件为文件夹"
If GetFileAttributes("C:\1.Bat") And 32 Then Print "此文件为保存属性"
End Sub

其中属性值:
0 一般
1 只读
2 隐藏
4 系统
16 文件夹
32 保存属性

要加多个属性的话可以写成: SetFileAttributes "C:\1.txt", 1 + 2 + 32

Cacls.exe 命令行下修改文件访问控制权限

不多写了自己慢慢去研究吧!!

嘎嘎

原文地址:https://www.cnblogs.com/wuyifu/p/2782687.html