问题描述:
已知文件路径 As String
需要设置为隐藏+系统属性,最好通过API完成 如果能通过Attrib也可以。
解析:
你可以闷和通过 Scripting.FileSystemObject 对象来设置指定文件的属性,它的 File 对象有一个 Attributes 可以由我们修改,测试代码及说明如下:
Private Sub Command1_Click()
Dim oFileSystem As Object
Dim oFile As Object
' 创建FSO对象
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
' 获取你想要改属性的文件
Set oFile = oFileSystem.GetFile("文件名")
' Normal 0 一般文件。未设置属性。
' ReadOnly 1 只读文件。属性为读/写。
' Hidden 2 隐藏文件。属性为读/写。
' System 4 系统文件。属性为读/写。
' 设置属性(以上列出了所有可更改的喊罩帆属性值)
' 将文件改为:只读郑雹+隐藏+系统
oFile.Attributes = 1 + 2 + 4
' 释放对象
Set oFile = Nothing
Set oFileSystem = Nothing
End Sub
你的问题可以这样解州慎决:首先你要使用vb
api函数以及api结构:
Public
Type
BY_HANDLE_FILE_INFORMATION
dwFileAttributes
As
Long
ftCreationTime
As
FILETIME
ftLastAccessTime
As
FILETIME
ftLastWriteTime
As
FILETIME
dwVolumeSerialNumber
As
Long
nFileSizeHigh
As
Long
nFileSizeLow
As
Long
nNumberOfLinks
As
Long
nFileIndexHigh
As
Long
nFileIndexLow
As
Long
End
Type
Public
Type
OFSTRUCT
cBytes
As
Byte
fFixedDisk
As
Byte
nErrCode
As
Integer
Reserved1
As
Integer
Reserved2
As
Integer
szPathName(OFS_MAXPATHNAME)
As
Byte
End
Type
Public
Declare
Function
OpenFile
Lib
"kernel32"
Alias
"OpenFile"
(ByVal
lpFileName
As
String,
lpReOpenBuff
As
OFSTRUCT,
ByVal
wStyle
As
Long)
As
Long
Public
Declare
Function
GetFileInformationByHandle
Lib
"kernel32"
Alias
"GetFileInformationByHandle"
(ByVal
hFile
As
Long,
lpFileInformation
As
BY_HANDLE_FILE_INFORMATION)
As
Long
关键的语句是:
dim
lp
as
OFSTRUCT
DIM
FILEINFO
AS
BY_HANDLE_FILE_INFORMATION
filehandle=openfile(“aa。pdf”,lp,OF_READ)
getfileinformationbyHandle
filehandle,FILEINFO
这个FILEINFO就是你要的文件的册迅敬所有属性的一个vb定义的结构,得到这个结构
你就可以得到文件所有的属性。
你还可以查阅vb
api函数的帮助,对你解决问题有帮助昌茄,祝好运!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)