c# - SQLite 的相对路径不适用于 WIX 工具集

标签 c# wpf sqlite wix

我正在使用 SQLite 数据库并使用 WIX 工具集创建 WPF 应用程序的安装程序。问题是,当我直接从 Visual Studio 运行时,下面的相对路径工作正常,但当我使用 WIX 创建安装程序时不起作用,并且在安装此安装程序运行程序后,它会给出数据库文件的 fatal error 。 在项目目录中,我创建了一个数据库文件夹,数据库文件位于其中,如下图所示:

enter image description here

通过WIX Toolset创建安装程序后,安装文件如下:

enter image description here

inventory_control.db 文件路径: enter image description here

dbConnectionString 路径:

enter image description here

我已经为相对路径连接字符串编写如下代码:

相对路径:

  string relativePath = @"Database\inventory_control.db";
        string currentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        //string path = currentPath.Substring(0, currentPath.Length - 21);
        string path = Path.GetDirectoryName(currentPath);
        string absolutePath = System.IO.Path.Combine(path, relativePath);
        string dbConnectionString = string.Format("Data Source={0};Version=3;Pooling=True;Max Pool Size=100;", absolutePath);

        //string dbConnectionString = "Data Source=inventory_control.db";
        sQLiteConnection = new SQLiteConnection(dbConnectionString);

dbConnectionString 提供正确的当前路径。 上面的相对路径在我直接从 Visual Studio 运行时工作正常,但在我使用 WIX 创建安装程序时不起作用。它给出了一个 fatal error 。如何解决?

WIX 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"><?define Inventory Control_TargetDir=$(var.Inventory Control.TargetDir)?>
    <Product Id="f941ba49-4369-44d4-aa0c-b77f20aa41db" Name="Inventory Control" Language="1033" Version="1.0.0.0" Manufacturer="devtros.com" UpgradeCode="ce092371-53cc-4be9-ab5d-c7a2685af970">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

    <Icon Id="app_icon.ico" SourceFile="$(var.ProjectDir)app_icon.ico" />
    <Property Id="ARPPRODUCTION" Value="app_icon.ico" />

    <WixVariable Id="WixUIBannerBmp" Value="Images\background.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="Images\background.bmp" />
    <WixVariable Id="WixUILicenseRtf" Value="$(var.ProjectDir)License.rtf" />

    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />
    <UIRef Id="WixUI_InstallDir" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate EmbedCab="yes" />

        <Feature Id="ProductFeature" Title="Inventory Control" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
      <ComponentRef Id="ApplicationShortcut" />
      <ComponentRef Id="ApplicationShortcutDesktop" />
            <ComponentGroupRef Id="Database_files" />
        </Feature>
    </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="Inventory Control">
          <Directory Id="Files" Name="Files" />
          <Directory Id="Database" Name="Database" />
        </Directory>
            </Directory>
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="Inventory Control" />
      </Directory>
      <Directory Id="DesktopFolder" Name="Desktop" />
        </Directory>
    </Fragment>

  <Fragment>
    <DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationShortcut" Guid="9bd13330-6540-406f-a3a8-d7f7c69ae7f9">
        <Shortcut Id="ApplicationStartMenuShortcut" Name="Inventory Control" Description="Inventory Control" Target="[INSTALLFOLDER]Inventory Control.exe" WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="RemoveApplicationProgramsFolder" Directory="ApplicationProgramsFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\Inventory Control" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="DesktopFolder">
      <Component Id="ApplicationShortcutDesktop" Guid="cde1e030-eb64-49a5-b7b8-400b379c2d1a">
        <Shortcut Id="ApplicationDesktopShortcut" Name="Inventory Control" Description="Inventory Control" Target="[INSTALLFOLDER]Inventory Control.exe" WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="RemoveDesktopFolder" Directory="DesktopFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\Inventory Control" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
  </Fragment>

    <Fragment>
        <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
            <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
            <!-- <Component Id="ProductComponent"> -->
                <!-- TODO: Insert files, registry keys, and other resources here. -->
            <!-- </Component> -->
            <Component Id="Inventory_Control.exe" Guid="0a7e7061-201b-4d49-adeb-4449e9c4da3e">
              <File Id="Inventory_Control.exe" Name="Inventory Control.exe" Source="$(var.Inventory Control_TargetDir)Inventory Control.exe" />
            </Component>
            <Component Id="Inventory_Control.exe.config" Guid="28323615-8159-4116-b1ac-e29a70bf2593">
              <File Id="Inventory_Control.exe.config" Name="Inventory Control.exe.config" Source="$(var.Inventory Control_TargetDir)Inventory Control.exe.config" />
            </Component>
            <Component Id="System.Windows.Controls.Input.Toolkit.dll" Guid="7d678201-767a-416b-b645-b2cb7d514893">
              <File Id="System.Windows.Controls.Input.Toolkit.dll" Name="System.Windows.Controls.Input.Toolkit.dll" Source="$(var.Inventory Control_TargetDir)System.Windows.Controls.Input.Toolkit.dll" />
            </Component>
            <Component Id="System.Data.SQLite.dll" Guid="178a5aef-c027-4215-81ae-f148ab6cd472">
              <File Id="System.Data.SQLite.dll" Name="System.Data.SQLite.dll" Source="$(var.Inventory Control_TargetDir)System.Data.SQLite.dll" />
            </Component>
            <Component Id="Zen.Barcode.Core.dll" Guid="20e34fc3-0066-4ffd-b401-518bc1177098">
              <File Id="Zen.Barcode.Core.dll" Name="Zen.Barcode.Core.dll" Source="$(var.Inventory Control_TargetDir)Zen.Barcode.Core.dll" />
            </Component>
            <Component Id="WPFToolkit.dll" Guid="8d974e65-defb-4675-b9e0-ff617e5ab1da">
              <File Id="WPFToolkit.dll" Name="WPFToolkit.dll" Source="$(var.Inventory Control_TargetDir)WPFToolkit.dll" />
            </Component>
        </ComponentGroup>
    </Fragment>

  <Fragment>
    <ComponentGroup Id="Database_files" Directory="Database">
      <Component Id="Database_inventory_control.db" Guid="0104b919-0aa9-4dc5-9492-14c474d97cf1">
        <File Id="Database_inventory_control.db" Name="inventory_control.db" Source="$(var.Inventory Control_TargetDir)Database\inventory_control.db" />
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

最佳答案

SQLite.Interop.dll: The file SQLite.Interop.dll needed to be installed along with the rest of the runtime files in order for SQLite to function properly.

There are two flavors of the file, x86 and x64 format. It is probably advisable to install both files in their respective folders:

Your installation folder hierarchy - mock-up:

  • YourBinary.exe
  • x86\SQLite.Interop.dll
  • x64\SQLite.Interop.dll
  • System.Data.SQLite.dll
  • Etc...

Read-Write DB Location: And then your database should be stored in a writeable path (or you need to make the path writeable for regular users using custom ACL permissioning - which is never a great idea).

Exceptions: Obviously try - catch your database connection, update and access code to detect these kinds of issues.


  1. 文件夹困惑:数据库文件夹中是否有数据库文件?它有点像 inventory_control.db 文件安装到主应用程序文件夹,而不是 Database 子文件夹?

    • 也许那个文件是由 application.exe 生成的在错误的文件夹中?
    • 或者您出于测试目的复制了主文件夹中的文件?
  2. Hard-Coded Dev-Box Sins?:它在 Inventory Control.exe.config 中说了什么? ?

    • 那里是否有可以覆盖您的代码值的相关设置?
    • 里面会不会有硬编码的开发箱问题?
  3. Path Builder:我假设您在启动期间将来自应用程序的路径“消息框化”,以确保它们是正确的?我喜欢复制路径并执行 Start => Run并粘贴路径以查看它是否打开。按CTRL + C当消息框显示时。粘贴到记事本中。提取路径在Start => Run中试试.

    • string path = currentPath.Substring(0, currentPath.Length - 21); .硬编码文件名中的字符数来获取父目录路径不是很健壮吗?
    • 你能用 Path.GetDirectoryName(currentPath) 改进它吗? ?
    • 或者甚至:string dir = currentPath.Substring(0,currentPath.LastIndexOf('\\'));
  4. 附加调试器和调试二进制文件?:也许您可以安装调试二进制文件并附加到它们以进行调试,如下所述:wix c# app doesn't launch after installing .只是为了获得真正的逐步调试 session 。

  5. 路径空间:该数据库连接字符串。它的路径需要引号吗?就像在“带空格的路径”中一样?您的 visual Studio 项目的路径中可能没有任何空格,但安装后路径中有空格。


源代码中的这个结构看起来很奇怪,为什么有必要?:

  • <?define Inventory Control_TargetDir=$(var.Inventory Control.TargetDir)?>

关于c# - SQLite 的相对路径不适用于 WIX 工具集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319558/

相关文章:

c# - 全局与成员函数

java - 无法使用光标成功从数据库检索 Spinner 选定项目文本以填充我的应用程序中的联系人列表项目

java - 如果字符串字段以加号 (+) 开头,更新 Sqlite 表?

python - 如何在Python中打印出电话号码?

c# - 如何使用 BeginExecuteReader

c# - 如何在三个类中处理事件

c# - Windows 桌面搜索 - SQL 令人难以置信的慢 '%search%'

wpf - ElementName 绑定(bind)失败

c# - C#打印条码 Crystal 报表

c# - 无法从 DataGrid 问题中转换 .SelectedItems