【问题标题】:Check right installation of 7-Zip (64 / 32bit)检查 7-Zip (64 / 32bit) 的正确安装
【发布时间】:2019-04-15 02:10:08
【问题描述】:

我的目标是避免来自 VBA 调试器的错误消息。需要检查安装了哪个版本的7-zip,程序文件/或程序文件(x86):

尝试做简单的“IF”函数。

Dim PathZipProgram As String
strCommand As String

PathZipProgram = "C:\Program Files(x86)\7-Zip\7z.exe"
If Right(PathZipProgram, 1) Then
    PathZipProgram = PathZipProgram
Else
    PathZipProgram = "C:\Program Files\7-Zip\7z.exe"
End If

Shell strCommand

strCommand = """" & PathZipProgram & """ a -tzip """

VBA 找不到 7zip。

【问题讨论】:

  • 您最好阅读注册表并获取安装路径。它应该存储在 HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\Path 下
  • 无 IF 功能,C:\Program Files\7-Zip\7z.exe 有效,无需阅读注册表。只寻找正确的 IF 函数如何选择 32 位或 64 位版本
  • 需要查看安装了哪个版本的7zip使用注册表

标签: vba 7zip


【解决方案1】:

您可以使用function 来检查文件是否存在,如下所示:

Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function

然后在你的代码中使用它:

Dim PathZipProgram As String
Dim strCommand As String

PathZipProgram = "C:\Program Files(x86)\7-Zip\7z.exe"

If Not FileExists(PathZipProgram) Then
    PathZipProgram = "C:\Program Files\7-Zip\7z.exe"
End If

Shell strCommand

strCommand = """" & PathZipProgram & """ a -tzip """

希望以此为起点。

【讨论】:

    【解决方案2】:

    安装路径:7-Zip 可以安装在另一个磁盘位置(例如 D: 驱动器或其他位置) .你做 需要从注册表中读取路径才能确定。几个 建议如下:


    1. Hacky Version

    我在下面放了一个完整的脚本来使用,但是你基本上可以得到你想要的 hacky 方式(确保路径确实存在 - 显然 - 如果有问题可以使用 "C:\Program Files\7-Zip"看过):

    Check File Exists:

    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FileExists("C:\Program Files (x86)\7-Zip\7z.exe")) Then
        ' Do Stuff
    End If
    

    Get File Version:

    Set fso = CreateObject("Scripting.FileSystemObject")
    MsgBox fso.GetFileVersion("C:\Program Files (x86)\7-Zip\7z.exe")
    

    请不要依赖这个。它最终会失败。请看下文。


    2. Full Version

    这是一个完整版本,您需要的步骤是制作希望可靠的东西。基本上从注册表中读取路径并从那里获取:

    Const HKEY_LOCAL_MACHINE = &H80000002 : strComputer = "."
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set reg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
    
    ' 64-bit - Read 7-zip installation path from registry
    regpath64 = "SOFTWARE\7-Zip"
    reg.GetStringValue HKEY_LOCAL_MACHINE, regpath64, "Path64", regvalue64
    fullpath64 = regvalue64 + "\" + "7z.exe"
    
    If (fso.FileExists(fullpath64)) Then
       MsgBox "7-zip 64-bit: " + fso.GetFileVersion(fullpath64), vbOKOnly, "64-bit:"
    End If
    
    ' 32-bit - Read 7-zip installation path from registry
    regpath32 = "SOFTWARE\WOW6432Node\7-Zip"
    reg.GetStringValue HKEY_LOCAL_MACHINE, regpath32, "Path", regvalue32
    fullpath32 = regvalue32 + "\" + "7z.exe"
    
    If (fso.FileExists(fullpath32)) Then
       MsgBox "7-zip 32-bit: " + fso.GetFileVersion(fullpath32), vbOKOnly, "32-bit:"
    End If
    

    免责声明:对于 64 位注册表读取:可能存在 Path 和 Path64 条目 (我都有)。不确定更早和/或更高版本会 有。请检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多