matteoh
matteoh

Reputation: 3630

How to embed external exe files in a Flutter project for Windows?

In my Flutter project for Windows, I want to run some .exe files that I want to embed in my project, instead of copying/pasting them by hand in the correct location.

So I tried to add the .exe files in my assets folder, like the following:

assets
  \exe
  \images

and add the exe folder in my pubspec.yaml file like the following:

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

The good news is: the exe files are copied in the correct location when I build my project. The bad news is: I don't know how to get the absolute path of my exe folder in Dart.

So, is there a way to get the absolute path of the exe folder for a Windows project, or is there another way to embed my .exe files so I can get their paths and run them (using Process.start())?

Thanks.

Upvotes: 11

Views: 2599

Answers (2)

hackengineer
hackengineer

Reputation: 111

If you are using flutter run you can just use this path

const exePath = "assets\\exe";

But if you plan on using flutter build you will need to use the approach by @matteoh

// Get the executable path and extract the parent directory
String appPath = Platform.resolvedExecutable;
appPath = appPath.substring(0, appPath.lastIndexOf("\\"));

String exePath = "$appPath\\data\\flutter_assets\\assets\\exe";

In my experimentation workingDirectory does not work with .exe files.

Process pythonApp = Process.run("api.exe", [], workingDirectory: exePath); 

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ProcessException: The system cannot find the file specified.

But if you call the exe directly like so

Process pythonApp = Process.run("$exePath\\api.exe", []); 

It works!

Upvotes: 1

matteoh
matteoh

Reputation: 3630

Here is what I did to get the absolute path of the exe folder (only tested on 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")

Upvotes: 5

Related Questions