如何在cx_freeze msi捆绑包中设置快捷方式工作目录?

3 投票
2 回答
2956 浏览
提问于 2025-04-18 09:37

我在做一个Python程序,这个程序是用来处理SQLite3数据库的。我用cx_Freeze把它做成了一个MSI安装文件。

通过这个MSI安装文件生成的Windows快捷方式,没有设置工作目录的属性。因此,当我通过桌面上的快捷方式运行这个程序时,它会把数据库文件直接创建在桌面上。

我可以通过给快捷方式设置一个不同的工作目录来解决这个问题。那么,我该怎么做呢?

2 个回答

1

另一个问题的回答中找到了这个解决方案。简单来说,需要设置快捷方式表的数据。下面的'TARGETDIR'是在shortcut_table中最后一个设置,它将工作目录设置为安装目录。

---摘自上述回答---

from cx_Freeze import *

# http://msdn.microsoft.com/en-us/library/windows/desktop/aa371847(v=vs.85).aspx
shortcut_table = [
    ("DesktopShortcut",        # Shortcut
     "DesktopFolder",          # Directory_
     "DTI Playlist",           # Name
     "TARGETDIR",              # Component_
     "[TARGETDIR]playlist.exe",# Target
     None,                     # Arguments
     None,                     # Description
     None,                     # Hotkey
     None,                     # Icon
     None,                     # IconIndex
     None,                     # ShowCmd
     'TARGETDIR'               # WkDir
     )
    ]

# Now create the table dictionary
msi_data = {"Shortcut": shortcut_table}

# Change some default MSI options and specify the use of the above defined tables
bdist_msi_options = {'data': msi_data}

setup(
    options = {
        "bdist_msi": bdist_msi_options,
    },
    executables = [
        Executable(
            "MyApp.py",
            )
        ]
    ) 
7

我通过对cx_Freeze/windist.py做了一个小改动,解决了这个问题。在add_config()的第61行,我把:

msilib.add_data(self.db, "Shortcut",
        [("S_APP_%s" % index, executable.shortcutDir,
                executable.shortcutName, "TARGETDIR",
                "[TARGETDIR]%s" % baseName, None, None, None,
                None, None, None, None)])

改成了

msilib.add_data(self.db, "Shortcut",
        [("S_APP_%s" % index, executable.shortcutDir,
                executable.shortcutName, "TARGETDIR",
                "[TARGETDIR]%s" % baseName, None, None, None,
                None, None, None, "TARGETDIR")]) # <--- Working directory.

谢谢大家。

撰写回答