如何在 VBA (MSAccess) 中检查文件加密

发布于 2025-01-04 23:36:05 字数 507 浏览 0 评论 0原文

我正在构建一种表单,根据数据库中的数据将文件从一个位置移动到另一个位置,只要源文件未加密(Windows 资源管理器中的绿色文件名)并且目标文件不存在,它就可以工作。

所以我试图创建以下内容:

Public Function isEncrypted(file As String) As Boolean
    Dim info As System.IO.FileInfo
    info = My.Computer.FileSystem.GetFileInfo(file)

    Dim attr As System.IO.FileAttributes
    attr = info.Attributes

    isEncrypted = ((attr And System.IO.FileAttributes.Encrypted) > 0)
End Function

但它根本不运行。任何人都有这方面的经验,或者有更简单的方法来检查加密吗?如果我尝试重命名和访问,访问会挂起并崩溃移动加密文件。

I'm building a form that moves files from one location to another based on data within the DB, and it works as long as the source file is not encrypted (green filename in windows explorer) and as long as the destination file does not exist.

So I'm trying to create the following:

Public Function isEncrypted(file As String) As Boolean
    Dim info As System.IO.FileInfo
    info = My.Computer.FileSystem.GetFileInfo(file)

    Dim attr As System.IO.FileAttributes
    attr = info.Attributes

    isEncrypted = ((attr And System.IO.FileAttributes.Encrypted) > 0)
End Function

But it doesn't run at all. Anyone have experience with this, or is there an easier way to check for encryption? Access hangs and crashes if I try to rename & move an encrypted file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-01-11 23:36:06

正如其他人提到的,vb.net 代码不可能在 access 中编译。您很可能需要使用 win32 的系统调用来获取文件属性。 FileSystemObject(Scrrun.dll“Windows 脚本运行时”)对象也将不起作用,因为它的枚举不会告诉您文件是否已加密。这是一个 Windows API 函数,您可以使用它来确定文件是否加密。

Public Enum FileAttribute
'uses VBA Hex Notation
    FILE_ATTRIBUTE_READONLY = &H1
    FILE_ATTRIBUTE_HIDDEN = &H2
    FILE_ATTRIBUTE_SYSTEM = &H4
    FILE_ATTRIBUTE_DIRECTORY = &H10
    FILE_ATTRIBUTE_ARCHIVE = &H20
    FILE_ATTRIBUTE_DEVICE = &H40
    FILE_ATTRIBUTE_NORMAL = &H80
    FILE_ATTRIBUTE_TEMPORARY = &H100
    FILE_ATTRIBUTE_SPARSE_FILE = &H200
    FILE_ATTRIBUTE_REPARSE_POINT = &H400
    FILE_ATTRIBUTE_COMPRESSED = &H800
    FILE_ATTRIBUTE_OFFLINE = &H1000
    FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
    FILE_ATTRIBUTE_ENCRYPTED = &H4000
    FILE_ATTRIBUTE_VIRTUAL = &H10000
End Enum

Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

Public Sub Test()

    Dim fileTestPath As String
    Dim attributes As Long

    fileTestPath = "C:\yourfile.txt"

    attributes = GetFileAttributes(fileTestPath)

    'uses bitwise AND calculations to determine values
    If (attributes And FileAttribute.FILE_ATTRIBUTE_ARCHIVE) Then Debug.Print "Archive"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_COMPRESSED) Then Debug.Print "Compressed"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_DEVICE) Then Debug.Print "Device"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_DIRECTORY) Then Debug.Print "Directory"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_ENCRYPTED) Then Debug.Print "Encrypted"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_HIDDEN) Then Debug.Print "Hidden"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_NORMAL) Then Debug.Print "Normal"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) Then Debug.Print "Not Content Indexed"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_OFFLINE) Then Debug.Print "Offline"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_READONLY) Then Debug.Print "ReadOnly"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_REPARSE_POINT) Then Debug.Print "ReparsePoint"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_SPARSE_FILE) Then Debug.Print "SparseFile"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_SYSTEM) Then Debug.Print "System"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_TEMPORARY) Then Debug.Print "Temporary"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_VIRTUAL) Then Debug.Print "Virtual"

End Sub

As others have mentioned, there is no way that the vb.net code is going to compile in access. You will most likely need to use a system call from win32 to get the file attributes. The FileSystemObject (Scrrun.dll 'Windows Scripting Runtime') object will not work either as it's enumeration will not tell you if the file is encrypted. Here is a windows API function you can use to determine if the file is encrypted or not.

Public Enum FileAttribute
'uses VBA Hex Notation
    FILE_ATTRIBUTE_READONLY = &H1
    FILE_ATTRIBUTE_HIDDEN = &H2
    FILE_ATTRIBUTE_SYSTEM = &H4
    FILE_ATTRIBUTE_DIRECTORY = &H10
    FILE_ATTRIBUTE_ARCHIVE = &H20
    FILE_ATTRIBUTE_DEVICE = &H40
    FILE_ATTRIBUTE_NORMAL = &H80
    FILE_ATTRIBUTE_TEMPORARY = &H100
    FILE_ATTRIBUTE_SPARSE_FILE = &H200
    FILE_ATTRIBUTE_REPARSE_POINT = &H400
    FILE_ATTRIBUTE_COMPRESSED = &H800
    FILE_ATTRIBUTE_OFFLINE = &H1000
    FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
    FILE_ATTRIBUTE_ENCRYPTED = &H4000
    FILE_ATTRIBUTE_VIRTUAL = &H10000
End Enum

Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

Public Sub Test()

    Dim fileTestPath As String
    Dim attributes As Long

    fileTestPath = "C:\yourfile.txt"

    attributes = GetFileAttributes(fileTestPath)

    'uses bitwise AND calculations to determine values
    If (attributes And FileAttribute.FILE_ATTRIBUTE_ARCHIVE) Then Debug.Print "Archive"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_COMPRESSED) Then Debug.Print "Compressed"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_DEVICE) Then Debug.Print "Device"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_DIRECTORY) Then Debug.Print "Directory"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_ENCRYPTED) Then Debug.Print "Encrypted"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_HIDDEN) Then Debug.Print "Hidden"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_NORMAL) Then Debug.Print "Normal"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) Then Debug.Print "Not Content Indexed"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_OFFLINE) Then Debug.Print "Offline"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_READONLY) Then Debug.Print "ReadOnly"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_REPARSE_POINT) Then Debug.Print "ReparsePoint"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_SPARSE_FILE) Then Debug.Print "SparseFile"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_SYSTEM) Then Debug.Print "System"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_TEMPORARY) Then Debug.Print "Temporary"
    If (attributes And FileAttribute.FILE_ATTRIBUTE_VIRTUAL) Then Debug.Print "Virtual"

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文