oleg-shilo / wixsharp

Framework for building a complete MSI or WiX source code by using script files written with C# syntax.
MIT License
1.12k stars 175 forks source link

Can't place a shortcut on the desktop to the main exe #1650

Open kglundgren opened 1 month ago

kglundgren commented 1 month ago

Hello,

As the title says, I can't place a shortcut on the desktop to the main exe. This is my code:

using System.Runtime.CompilerServices;
using WixSharp;
using File = WixSharp.File;

[assembly: InternalsVisibleTo(assemblyName: "WixSharp_MSI_Installer.aot")] // assembly name + '.aot suffix

static string GetRootDir([CallerFilePath] string filePath = "") =>
    $@"{Path.GetDirectoryName(filePath)!}\..";

var projectName = "MyApp";

string rootDir = GetRootDir();
var appPublishDir = $@"{rootDir}\build\app-x64-rel";
var buildDir = $@"{rootDir}\build\wix";

if (!Directory.Exists(buildDir)) Directory.CreateDirectory(buildDir);
Directory.GetFiles(buildDir).ForEach(System.IO.File.Delete);

var installDir = $@"%ProgramFiles%\MyOrg AB\{projectName}";
Id installDirId = new("INSTALLDIR");

var mainExeFileName = $"{projectName}.exe";
var mainExePath = $@"{appPublishDir}\{projectName}";
Id mainExeId = new("MainExe");

var installerResources = $@"{rootDir}\WixSharp_MSI_Installer\Resources";

Console.WriteLine($"main exe id: {mainExeId}");

var project = new ManagedProject(projectName,
    new Dir(id: installDirId, targetPath: installDir,
        new Files(@"*.*", f => !f.EndsWith(mainExeFileName)),
        new File(id: mainExeId, sourcePath: mainExePath,
            new FileShortcut(name: projectName, location: "%Desktop%")
            {
                WorkingDirectory = installDir,
            })
        ))
{
    Platform = Platform.x64,
    GUID = new Guid("33f767ca-a96f-48f7-9fc9-30b2aa777489"),
    SourceBaseDir = appPublishDir,
    OutDir = buildDir,
    LicenceFile = $@"{installerResources}\CustomLicense.rtf",
};

project.BuildMsi();

That gives me this warning and error:

warning WIX1077: The 'Shortcut.MainExe.MyApp_.WorkDir' Property contains '[ProgramFilesFolder]' in its value which is an illegal reference to another property. If this value is a string literal, not a property reference, please ignore this warning. To set a property with the value of another property, use a CustomAction with Property and Value attributes.
error WIX0094: The identifier 'Directory:Shortcut.MainExe.MyApp_.WorkDir' could not be found. Ensure you have typed the reference correctly and that all the necessary inputs are provided to the linker.

I tried changing WorkingDirectory of the shortcut to the installDirId and then I don't get any errors but the shortcut is not created.

I've tried looking for the documentation on how to create shortcuts but haven't been able to find any, if there are I apologize. I tried looking at the samples and copying them but they didn't help either.

kglundgren commented 1 month ago

I tried changing the code to this, using ExeFileShortcut instead, but it still doesn't work:

using System.Runtime.CompilerServices;
using WixSharp;
using File = WixSharp.File;

[assembly: InternalsVisibleTo(assemblyName: "WixSharp_MSI_Installer.aot")] // assembly name + '.aot suffix

static string GetRootDir([CallerFilePath] string filePath = "") =>
    $@"{Path.GetDirectoryName(filePath)!}\..";

var projectName = "MyApp";
var mainExe = $"{projectName}.exe";

string rootDir = GetRootDir();
var appPublishDir = $@"{rootDir}\build\app-x64-rel";
var buildDir = $@"{rootDir}\build\wix";

if (!Directory.Exists(buildDir)) Directory.CreateDirectory(buildDir);
Directory.GetFiles(buildDir).ForEach(System.IO.File.Delete);

var installerResources = $@"{rootDir}\WixSharp_MSI_Installer\Resources";

var project = new ManagedProject(projectName,
    new Dir($@"%ProgramFiles%\MyOrg AB\{projectName}",
        new Files(@"*.*", f => !f.EndsWith(mainExe)),
        new File(sourcePath: $@"{appPublishDir}\{mainExe}")),
    new Dir("%Desktop%",
        new ExeFileShortcut(name: projectName, target: $@"[INSTALLDIR]{mainExe}", arguments: "")
        {
            WorkingDirectory = "[INSTALLDIR]" }
        ))
{
    Platform = Platform.x64,
    GUID = new Guid("33f767ca-a96f-48f7-9fc9-30b2aa777489"),
    SourceBaseDir = appPublishDir,
    OutDir = buildDir,
    LicenceFile = $@"{installerResources}\CustomLicense.rtf",
};

project.BuildMsi();
kglundgren commented 1 month ago

Wait, it does work... The shortcuts are just placed in C:\Users\Public\Desktop. I had no idea this directory even existed!