Skip to content

How To Create Desktop Shortcut

Yehuda Kremer edited this page Nov 29, 2021 · 2 revisions

Msix installer not support desktop shortcuts

But you can create desktop shortcut file (.link) programmatically. first check if the app is running for the first time, if so, create desktop shortcut.

  1. download .lnk file: MyAppName.zip
  2. edit the .lnk file name to what ever you want
  3. right click on the .lnk file and open properties, edit the field Target: change the "...[flutter_again_2].exe" to your app name (same value as name: from your pubspec.yaml), and save
  4. create assets folder called "myAssets" in your project folder, and copy to it the .lnk file
  5. use the latest version of msix package: 2.6.4 or later
  6. add to your msix_config:
  add_execution_alias: true
  assets_directory_path: 'C:\Users\me\Desktop\your_project_path\myAssets'
  1. edit your main.dart:
Future<void> _moveLnkFileToDesktop() async {
  // "my_app_name" is the value of "name:" in your pubspec.yaml
  String myAppDataPath = '${Platform.environment['AppData']}\\my_app_name';

  try {
    var myAppDataDirectoryExists = await Directory(myAppDataPath).exists();

    //if myAppDataDirectoryExists is false then the app is running for the first time
    if (!myAppDataDirectoryExists) {
      await Directory(myAppDataPath).create();
      await File('${File(Platform.resolvedExecutable).parent.path}\\myAssets\\MyAppName.lnk')
          .copy('${Platform.environment['USERPROFILE']}\\desktop\\MyAppName.lnk');
    }
  } catch (e) {
    //log
  }
}

void main() {
  runApp(MyApp());
  _moveLnkFileToDesktop();
}

in this code update your app name and the .lnk file name

And that is, this function will copy the .lnk file to the user desktop, only the first time the app will open (usually after the msix is finish installing)

TA DA: image

Clone this wiki locally