C# c++ 实现程序开机自启动

C# c++ 实现程序开机自启动

1、C#

1、使用注册表

cpp 复制代码
using Microsoft.Win32;

public static void SetAutoStart(bool enable)
{
    RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    if (enable)
    {
        rk.SetValue("MyApp", Application.ExecutablePath);
    }
    else
    {
        rk.DeleteValue("MyApp", false);
    }
    rk.Close();
}

2、使用启动文件夹

cpp 复制代码
using System;
using System.IO;
using IWshRuntimeLibrary;

public static void SetAutoStartByStartupFolder(bool enable)
{
    string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
    string shortcutPath = Path.Combine(startupFolder, "MyApp.lnk");

    if (enable)
    {
        // 创建快捷方式
        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
        shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        shortcut.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        shortcut.Description = "My Application";
        shortcut.Save();
    }
    else
    {
        // 删除快捷方式
        if (File.Exists(shortcutPath))
        {
            File.Delete(shortcutPath);
        }
    }
}

2、C++

1、使用注册表

cpp 复制代码
#include <Windows.h>
#include <string>

bool SetAutoStart(bool enable) {
    HKEY hKey;
    LPCTSTR lpSubKey = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
    LONG lResult = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_SET_VALUE, &hKey);
    if (lResult != ERROR_SUCCESS) {
        return false;
    }

    TCHAR szExePath[MAX_PATH];
    GetModuleFileName(NULL, szExePath, MAX_PATH);

    if (enable) {
        // 设置注册表值,程序路径
        lResult = RegSetValueEx(hKey, TEXT("MyApp"), 0, REG_SZ, (BYTE*)szExePath, (lstrlen(szExePath) + 1) * sizeof(TCHAR));
    } else {
        // 删除注册表值
        lResult = RegDeleteValue(hKey, TEXT("MyApp"));
    }

    RegCloseKey(hKey);
    return lResult == ERROR_SUCCESS;
}

2、使用启动文件夹

cpp 复制代码
#include <Windows.h>
#include <ShlObj.h>
#include <string>

bool SetAutoStartByStartupFolder(bool enable) {
    TCHAR szStartupPath[MAX_PATH];
    TCHAR szExePath[MAX_PATH];
    TCHAR szLinkPath[MAX_PATH];

    // 获取启动文件夹路径
    if (FAILED(SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, 0, szStartupPath))) {
        return false;
    }

    // 获取当前程序路径
    GetModuleFileName(NULL, szExePath, MAX_PATH);

    // 构造快捷方式路径
    _stprintf_s(szLinkPath, MAX_PATH, TEXT("%s\\MyApp.lnk"), szStartupPath);

    if (enable) {
        // 创建快捷方式
        // 使用Shell API创建快捷方式
        HRESULT hres;
        IShellLink* pShellLink = NULL;
        IPersistFile* pPersistFile = NULL;

        hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShellLink);
        if (SUCCEEDED(hres)) {
            pShellLink->SetPath(szExePath);
            pShellLink->SetDescription(TEXT("My Application"));

            hres = pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile);
            if (SUCCEEDED(hres)) {
                pPersistFile->Save(szLinkPath, TRUE);
                pPersistFile->Release();
            }
            pShellLink->Release();
        }
        return SUCCEEDED(hres);
    } else {
        // 删除快捷方式
        return DeleteFile(szLinkPath) != 0;
    }
}
相关推荐
Thera7774 分钟前
【Linux C++】彻底解决僵尸进程:waitpid(WNOHANG) 与 SA_NOCLDWAIT
linux·服务器·c++
3GPP仿真实验室6 分钟前
【Matlab源码】6G候选波形:OFDM-IM 增强仿真平台 DM、CI
开发语言·matlab·ci/cd
Wei&Yan8 分钟前
数据结构——顺序表(静/动态代码实现)
数据结构·c++·算法·visual studio code
devmoon10 分钟前
在 Polkadot 上部署独立区块链Paseo 测试网实战部署指南
开发语言·安全·区块链·polkadot·erc-20·测试网·独立链
lili-felicity10 分钟前
CANN流水线并行推理与资源调度优化
开发语言·人工智能
沐知全栈开发11 分钟前
CSS3 边框:全面解析与实战技巧
开发语言
island131421 分钟前
CANN GE(图引擎)深度解析:计算图优化管线、内存静态规划与异构 Stream 调度机制
c语言·开发语言·神经网络
wregjru23 分钟前
【QT】4.QWidget控件(2)
c++
曹牧25 分钟前
Spring Boot:如何在Java Controller中处理POST请求?
java·开发语言
浅念-28 分钟前
C++入门(2)
开发语言·c++·经验分享·笔记·学习