弹来弹去跑马灯!

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

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

//by wgscd

//date 2024-10-22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
        }
 
 
 
 
 
    }
}

  

调用:

1
2
3
4
if (!AppShortcutHelper.Exists())
{
    AppShortcutHelper.CreateShortcut();
}

  

 

posted @   wgscd  阅读(103)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示