如何将外部exe文件嵌入Flutter项目中以供Windows使用?

9
在我的Flutter Windows项目中,我想要运行一些.exe文件,我希望将它们嵌入到我的项目中,而不是手动复制/粘贴到正确的位置。因此,我尝试将.exe文件添加到我的assets文件夹中,如下所示:
assets
  \exe
  \images

并在我的pubspec.yaml文件中添加exe文件夹,如下所示:

flutter:
  assets:
    - assets/images/
    - assets/exe/

好消息是: 当我构建我的项目时,exe文件会被复制到正确的位置。

坏消息是: 我不知道如何在Dart中获取我的exe文件夹的绝对路径。

那么,有没有一种方法可以为Windows项目获取exe文件夹的绝对路径,或者有另外一种方法可以嵌入我的.exe文件,以便我可以获取它们的路径并运行它们(使用Process.start())?

谢谢。


1
你面对同样的问题了吗?你找到任何解决方案了吗? - Atul Chaudhary
@AtulChaudhary 我下面的回答 - matteoh
这里是一个可能的答案(提示:你无法获取文件路径):https://dev59.com/P1QK5IYBdhLWcg3wKcxM#63995005 - Pablo Insua
我也遇到了同样的问题,我的解决方法是将exe文件包含在安装程序内部(而不是项目内部)。在我的情况下,我使用了Flutter的Msix安装程序。 - Pablo Insua
你找到任何解决方案了吗? - gowthaman C
@gowthamanC 我的回答如下 - matteoh
1个回答

4

以下是我获取exe文件夹绝对路径的步骤(仅在Windows上测试过):

// returns the abolute path of the executable file of your app:
String mainPath = Platform.resolvedExecutable;

// remove from that path the name of the executable file of your app:
mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\"));

// concat the path with '\data\flutter_assets\assets\exe', where 'exe' is the
// directory where are the executable files you want to run from your app:
Directory directoryExe = Directory("$mainPath\\data\\flutter_assets\\assets\\exe")

我在Linux上遇到了一个问题(我认为在Windows上可能会类似):当您尝试启动exe文件的进程时,它会抛出“权限被拒绝”的异常。 - Pablo Insua
@PabloInsua 请尝试这个命令,chmod a+x yourfile。 - Kylin.Zhang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接