弹来弹去跑马灯!

C# 给当前程序创建桌面快捷方式

C# 给当前程序创建桌面快捷方式

//by wgscd

//date 2024-10-22

using System;
using System.Reflection;
using System.IO;
namespace TestApp
{  
   public  class AppShortcutHelper
    {
 

        private static string lnkFullPath  = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Config.softTitle + ".lnk");
        /// <summary>
        /// 给当前程序创建快捷方式
        /// </summary>
        /// <param name="lnkFullPath">快捷方式绝对路径</param>
        /// <param name="startupArgs">快捷方式启动参数</param>
        public static void CreateShortcut()
        {
            try
            {
                string  exeName = $"{Assembly.GetExecutingAssembly().GetName().Name}.exe"; 
                var exeDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                var shellType = Type.GetTypeFromProgID("WScript.Shell");
                dynamic shell = Activator.CreateInstance(shellType);
                var shortcut = shell.CreateShortcut(lnkFullPath);
                // 工作目录和目标路径可以自由指定,注意TargetPath必须是exe的绝对路径
                shortcut.WorkingDirectory = exeDir;
                shortcut.TargetPath = Path.Combine(exeDir, exeName);
                shortcut.Arguments = "";
                shortcut.Save();
            }
            catch 
            { 
            
            }

        }
        public static bool  Exists()
        {
            return File.Exists(lnkFullPath);
        }





    }
}

  

调用:

  if (!AppShortcutHelper.Exists())
  {
      AppShortcutHelper.CreateShortcut();
  }

  

 

posted @ 2024-10-22 10:29  wgscd  阅读(43)  评论(0编辑  收藏  举报