C# 对桌面快捷方式的操作设置开机启动项

首先在项目中引入Windows Script Host Object Model,引入方式如下图。

对于桌面快捷方式的修改无非就是将现有的快捷方式修改和添加新的快捷方式。

1、遍历桌面快捷方式,代码如下。

cs 复制代码
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string[] files = Directory.GetFiles(desktopPath, "*.lnk");

            foreach (string file in files)
            {
                WshShell shell = new WshShell();
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(file);

                Debug.WriteLine($"快捷方式名称: {Path.GetFileNameWithoutExtension(file)}");
                Debug.WriteLine($"目标路径: {shortcut.TargetPath}");
                Debug.WriteLine($"工作目录: {shortcut.WorkingDirectory}");
                Debug.WriteLine($"描述: {shortcut.Description}");
                Debug.WriteLine($"图标位置: {shortcut.IconLocation}");
                //Debug.WriteLine();
            }

2、创建一个快捷方式,代码如下。

cs 复制代码
            // 创建WScript.Shell对象
            WshShell _shell = new WshShell();

            // 创建快捷方式
            IWshShortcut _shortcut = (IWshShortcut)_shell.CreateShortcut(@"C:\Users\Public\Desktop\来个快捷方式.lnk");

            // 设置快捷方式的属性
            _shortcut.TargetPath = @"C:\Program Files\ScreenToGif\ScreenToGif.exe";
            _shortcut.WorkingDirectory = @"C:\Program Files\ScreenToGif";
            _shortcut.Description = "来个快捷方式";
            _shortcut.IconLocation = "C:\\Program Files\\ScreenToGif\\ScreenToGif.exe,0";

            // 保存快捷方式
            _shortcut.Save();

            Debug.WriteLine("快捷方式已创建。");

3、设置开机启动项。

cs 复制代码
            //AppDomain.CurrentDomain.BaseDirectory 获取当前程序所在文件夹
            //Application.Current.Shutdown(); 退出当前程序
            
            string appPath = @"C:\Program Files\ScreenToGif\ScreenToGif.exe"; // 替换为你的应用程序路径
            string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";

            RegistryKey key = Registry.CurrentUser.CreateSubKey(keyName);
            if (key != null)
            {
                key.SetValue("来个快捷方式", appPath); // "YourAppName"是注册表项的名称
                key.Close();
                Debug.WriteLine("应用程序已设置为开机启动。");
            }

4、有些时候设置好桌面的快捷方式,但是不能马上显示,需要刷新一下桌面才行,下面的代码是刷新桌面的代码。

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace XXXX
{
    public class DesktopRefurbish
    {
        /// <summary>
        /// 桌面刷新
        /// </summary>
        [DllImport("shell32.dll")]
        public static extern void SHChangeNotify(HChangeNotifyEventID wEventId, HChangeNotifyFlags uFlags, IntPtr dwItem1, IntPtr dwItem2);
        public static void DeskRef()
        {
            SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
        }
    }
    #region public enum HChangeNotifyFlags
    [Flags]
    public enum HChangeNotifyFlags
    {
        SHCNF_DWORD = 0x0003,
        SHCNF_IDLIST = 0x0000,
        SHCNF_PATHA = 0x0001,
        SHCNF_PATHW = 0x0005,
        SHCNF_PRINTERA = 0x0002,
        SHCNF_PRINTERW = 0x0006,
        SHCNF_FLUSH = 0x1000,
        SHCNF_FLUSHNOWAIT = 0x2000
    }
    #endregion//enum HChangeNotifyFlags
    #region enum HChangeNotifyEventID
    [Flags]
    public enum HChangeNotifyEventID
    {
        SHCNE_ALLEVENTS = 0x7FFFFFFF,
        SHCNE_ASSOCCHANGED = 0x08000000,
        SHCNE_ATTRIBUTES = 0x00000800,
        SHCNE_CREATE = 0x00000002,
        SHCNE_DELETE = 0x00000004,
        SHCNE_DRIVEADD = 0x00000100,
        SHCNE_DRIVEADDGUI = 0x00010000,
        SHCNE_DRIVEREMOVED = 0x00000080,
        SHCNE_EXTENDED_EVENT = 0x04000000,
        SHCNE_FREESPACE = 0x00040000,
        SHCNE_MEDIAINSERTED = 0x00000020,
        SHCNE_MEDIAREMOVED = 0x00000040,
        SHCNE_MKDIR = 0x00000008,
        SHCNE_NETSHARE = 0x00000200,
        SHCNE_NETUNSHARE = 0x00000400,
        SHCNE_RENAMEFOLDER = 0x00020000,
        SHCNE_RENAMEITEM = 0x00000001,
        SHCNE_RMDIR = 0x00000010,
        SHCNE_SERVERDISCONNECT = 0x00004000,
        SHCNE_UPDATEDIR = 0x00001000,
        SHCNE_UPDATEIMAGE = 0x00008000,
    }
    #endregion
}

5、关闭防火墙,代码如下。

cs 复制代码
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = "netsh",
                    Arguments = "advfirewall set allprofiles state off",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                };

                Process process = Process.Start(psi);
                process.WaitForExit();

                Debug.WriteLine("Windows 防火墙已关闭。");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("关闭 Windows 防火墙时出错: " + ex.Message);
            }
相关推荐
Sylvia-girl1 小时前
Java——抽象类
java·开发语言
Yana.nice3 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时4 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon4 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
tomorrow.hello5 小时前
Java并发测试工具
java·开发语言·测试工具
晓13135 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊5 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
她说人狗殊途7 小时前
java.net.InetAddress
java·开发语言
天使day7 小时前
Cursor的使用
java·开发语言·ai
喵叔哟7 小时前
3. 【Blazor全栈开发实战指南】--Blazor是什么?为什么选择Blazor?
c#·.netcore