How to create a desktop shortcut?

I have a .net 9 Maui windows application, and need to create a desktop shortcut for it. The code I would normally use for this is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public static void CreateDesktopShortcut()
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string shortcutPath = Path.Combine(desktopPath, "thing.lnk");
var shell = new Shell32.Shell();
var shortcut = (Shell32.ShellLinkObject)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = "path.exe";
shortcut.Description = "interesting description";
shortcut.Save();
}
</code>
<code> public static void CreateDesktopShortcut() { string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string shortcutPath = Path.Combine(desktopPath, "thing.lnk"); var shell = new Shell32.Shell(); var shortcut = (Shell32.ShellLinkObject)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = "path.exe"; shortcut.Description = "interesting description"; shortcut.Save(); } </code>
 public static void CreateDesktopShortcut()
 {
     string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
     string shortcutPath = Path.Combine(desktopPath, "thing.lnk");

     var shell = new Shell32.Shell();
     var shortcut = (Shell32.ShellLinkObject)shell.CreateShortcut(shortcutPath);

     shortcut.TargetPath = "path.exe";
     shortcut.Description = "interesting description";

     shortcut.Save();
 }

However Shell32 isn’t found though. Is there a new way of creating desktop shortcuts when running a maui application?

Further info:
Adding “Microsoft Shell Controls and Automation” as a com reference, Shell32.Shell exists, but does not contain the method “CreateShortcut”

You can refer to this official document about How to create a desktop shortcut with the Windows Script Host.

  1. Add the COM reference by adding the following code into the csproj file or you can add it by Project > Add > COM Reference > Windows Script Host Object Model.:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>      <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
       <COMReference Include="IWshRuntimeLibrary">
       <WrapperTool>tlbimp</WrapperTool>
       <VersionMinor>0</VersionMinor>
       <VersionMajor>1</VersionMajor>
       <Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
       <Lcid>0</Lcid>
       <Isolated>false</Isolated>
       <EmbedInteropTypes>true</EmbedInteropTypes>
       </COMReference>
      </ItemGroup>
</code>
<code>      <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">        <COMReference Include="IWshRuntimeLibrary">        <WrapperTool>tlbimp</WrapperTool>        <VersionMinor>0</VersionMinor>        <VersionMajor>1</VersionMajor>        <Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>        <Lcid>0</Lcid>        <Isolated>false</Isolated>        <EmbedInteropTypes>true</EmbedInteropTypes>        </COMReference>       </ItemGroup> </code>
      <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
        <COMReference Include="IWshRuntimeLibrary">
          <WrapperTool>tlbimp</WrapperTool>
          <VersionMinor>0</VersionMinor>
          <VersionMajor>1</VersionMajor>
          <Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
          <Lcid>0</Lcid>
          <Isolated>false</Isolated>
          <EmbedInteropTypes>true</EmbedInteropTypes>
        </COMReference>
      </ItemGroup>
  1. Declare the Helper class in the /Platforms/Windows:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>namespace MauiApp.Platforms.Windows
{
public class Helper
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags,
[Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpExeName,
ref uint lpdwSize);
public void CreateShortcut()
{
// Get a handle to the current process
IntPtr hProcess = Process.GetCurrentProcess().Handle;
uint dwFlags = 0;
StringBuilder lpExeName = new StringBuilder(260);
uint lpdwSize = (uint)lpExeName.Capacity;
//Get exe path
bool result = QueryFullProcessImageName(hProcess, dwFlags, lpExeName, ref lpdwSize);
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"MauiApp.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for a MauiApp";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = lpExeName.ToString();
shortcut.Save();
}
}
}
</code>
<code>namespace MauiApp.Platforms.Windows { public class Helper { [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpExeName, ref uint lpdwSize); public void CreateShortcut() { // Get a handle to the current process IntPtr hProcess = Process.GetCurrentProcess().Handle; uint dwFlags = 0; StringBuilder lpExeName = new StringBuilder(260); uint lpdwSize = (uint)lpExeName.Capacity; //Get exe path bool result = QueryFullProcessImageName(hProcess, dwFlags, lpExeName, ref lpdwSize); object shDesktop = (object)"Desktop"; WshShell shell = new WshShell(); string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"MauiApp.lnk"; IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); shortcut.Description = "New shortcut for a MauiApp"; shortcut.Hotkey = "Ctrl+Shift+N"; shortcut.TargetPath = lpExeName.ToString(); shortcut.Save(); } } } </code>
namespace MauiApp.Platforms.Windows
{
    public class Helper
    {
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags,
                       [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpExeName,
                       ref uint lpdwSize);
        public void CreateShortcut()
        {
            // Get a handle to the current process
            IntPtr hProcess = Process.GetCurrentProcess().Handle;
            uint dwFlags = 0;
            StringBuilder lpExeName = new StringBuilder(260);
            uint lpdwSize = (uint)lpExeName.Capacity;
            //Get exe path
            bool result = QueryFullProcessImageName(hProcess, dwFlags, lpExeName, ref lpdwSize);

            object shDesktop = (object)"Desktop";
            WshShell shell = new WshShell();
            string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"MauiApp.lnk";
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "New shortcut for a MauiApp";
            shortcut.Hotkey = "Ctrl+Shift+N";
            shortcut.TargetPath = lpExeName.ToString();
            shortcut.Save();
        }
    }
}

And then you can new the Helper.cs and call the CreateShortcut() method.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật