在我的 Windows Flutter 项目中,我想运行一些要嵌入到项目中的
.exe
文件,而不是手动将它们复制/粘贴到正确的位置。
所以我尝试将
.exe
文件添加到我的 assets
文件夹中,如下所示:
assets
\exe
\images
并在我的
exe
文件中添加 pubspec.yaml
文件夹,如下所示:
flutter:
assets:
- assets/images/
- assets/exe/
好消息是:当我构建项目时,exe 文件被复制到正确的位置。 坏消息是:我不知道如何获取 Dart 中
exe
文件夹的绝对路径。
那么,有没有办法获取 Windows 项目的
exe
文件夹的绝对路径,或者是否有另一种方法来嵌入我的 .exe
文件,以便我可以获得它们的路径并运行它们(使用 Process.start()
) ?
谢谢。
这是我获取
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")
首先,Process.run 似乎在与资产文件夹相同的父目录中启动,因此可执行文件的路径是
const exePath = "assets\\exe";
在我的实验中,workingDirectory 不适用于 .exe 文件。
Process pythonApp = Process.run("api.exe", [], workingDirectory: exePath);
[错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理 异常:ProcessException:系统找不到该文件 指定。
但是如果你像这样直接调用exe
Process pythonApp = Process.run("$exePath\\api.exe", []);
有效!