首页 > 编程语言 >C# 推荐一种开机自启动的方式

C# 推荐一种开机自启动的方式

时间:2024-07-29 09:22:17浏览次数:8  
标签:string ShortcutPath C# startup shortcut 开机 自启动

  • 概述(Overview)

  网上多数搜索结果以注册表设置为优先,这个方法需要管理员权限,实际工作中可能并不适用。这个方法是直接写到用户开机自启动目录里,系统开机会带着一起启动。(Most search results on the web are based on registry preferences, which requires administrator privileges and may not be applicable in practice. This method is written directly to the user's boot directory, and the system boot will be started with it.)

  • 代码(Code):

/// <summary>
/// 设置程序自动运行
/// </summary>
/// <param name="IsAppAutoStart">是否开机自启动</param>
public static bool SetAppAutoRun(bool IsAppAutoStart=true)
{
        string sourseEXE = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestApp.exe");
        string startup_ShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\TestApp.lnk";
        if (IsAppAutoStart)//需要创建开机自启动
        {
            if (File.Exists(startup_ShortcutPath))
            {
                File.Delete(startup_ShortcutPath);
            }
            CreateShortcut(sourseEXE, startup_ShortcutPath);
        }
        else
        {
            if (File.Exists(startup_ShortcutPath))
            {
                File.Delete(startup_ShortcutPath);
            }
        }
}

/// <summary>
/// 创建一个快捷方式(用户级别)
/// </summary>
/// <param name="targetPath">目标程序路径.exe</param>
/// <param name="shortcutPath">快捷方式放置路径.lnk</param>
private static bool CreateShortcut(string targetPath, string shortcutPath)
{
    try
    {
     //反射 var shellType = Type.GetTypeFromProgID("WScript.Shell");//这是windows自带的快捷方式生成程序,可以直接借用 dynamic shell = Activator.CreateInstance(shellType); var shortcut = shell.CreateShortcut(shortcutPath); shortcut.TargetPath = targetPath; //shortcut.Arguments = "在路径后面追加的参数 空格分隔"; //shortcut.Description = "备注信息"; //shortcut.WindowStyle = FormWindowState.Normal; //shortcut.IconLocation = "图标路径"; shortcut.Save(); return true; } catch (Exception ex) { return false; } }

标签:string,ShortcutPath,C#,startup,shortcut,开机,自启动
From: https://www.cnblogs.com/tpen/p/18329327

相关文章

  • React实现过渡效果更新时间展示
     创建一个组件,实时展示时分秒,并且动态更新数据。数据变化时利用过渡效果动态更新。利用两个元素,重叠在一个位置,以达到交替变化的效果//创建秒显示节点<divclassName="textsecond"> <CSSTransition in="{renderSecond}" timeout="{1000}" classNames="fade" unmo......
  • C/C++ 头文件注意事项总结
    C/C++头文件在编程中扮演着至关重要的角色,它们用于声明函数、类、宏、常量等,使得这些声明可以在多个源文件中共享。然而,在使用头文件时,需要注意一些关键事项以避免编译错误、提高代码的可维护性和可读性。以下是一些关于C/C++头文件使用的注意事项:1.防止头文件重复包含头文......
  • netcore日志与异常
    ILogger简单使用asp.netcore的webApplicationBuilder中自带了一个日志组件。无需手动注册服务就能直接在控制器中构造注入。publicHomeController(ILogger<HomeController>logger){_logger=logger;}_logger.LogTrace("trace{path}",HttpContext.Request.Path);_......
  • SQL Server 2022 RTM Cumulative Update 14 发布 (累积更新包)
    SQLServer2022RTMCumulativeUpdate14发布(累积更新包)请访问原文链接:https://sysin.org/blog/sql-server-2022/,查看最新版。原创作品,转载请保留出处。SQLServer2022SQLServer2022现已普遍可用2022年11月16日,巨硬宣布正式发布SQLServer2022,这是迄今为止......
  • VirtualBox 7.0.20 (macOS, Linux, Windows) - 开源跨平台虚拟化软件
    VirtualBox7.0.20(macOS,Linux,Windows)-开源跨平台虚拟化软件OracleVMVirtualBox7请访问原文链接:https://sysin.org/blog/virtualbox-7/,查看最新版。原创作品,转载请保留出处。VirtualBox简介VirtualBox是一款功能强大的x86和AMD64/Intel64虚拟化产品,适用于......
  • 如果 频繁操作Hbase 数据库 ,可以保持 Connection connection 一直连接吗
    问:如果频繁操作Hbase数据库,可以保持 Connectionconnection一直连接吗,或者有什么方式可以优化吗,如何 使用连接池(HBase自己提供的连接池)可以复用连接对象,减少频繁创建和销毁连接的开销。连接池负责维护一组预先建立的连接,并在需要时将它们提供给应用程序。当应用程序完成操......
  • dao层 和controller
    voidinsertReservation(Reservationreservation);voiddeleteReservation(intreservationId);ReservationselectReservation(intreservationId);List<Reservation>selectAllReservations();voidupdateReservation(Reservationreservation);List<Reservation&......
  • HV000030: No validator could be found for constraint ‘javax.validation.constrai
    原文链接:https://blog.csdn.net/miachen520/article/details/119817478错误原因:数字类型不能使用javax.validation.constraints.Pattern注解解决办法方法一:.将字段类型设置为String类型;方法二:使用其他验证注解验证,数字的有@Size,@Min,@Max,@Range现将@Valid常用注解介绍如下:Be......
  • 【PyCharm】PyCharm 2024.1 的最新变化-版本控制集成
    目录更强大的VCS支持Git、SVN和Mercurial的改进分支管理冲突解决提交历史更强大的VCS支持PyCharm2024.1在版本控制系统的集成方面进行了显著的改进,增强了对Git、Subversion(SVN)和Mercurial的支持。这些改进旨在提高开发者的效率,并使版本控制操作更加直......
  • Solution - Atcoder ABC280Ex Substring Sort
    对于这种子串问题,且有多个基础串,一个比较直观的想法就是先上个广义SAM。考虑SAM与字典序如何联系上。因为跳\(\operatorname{fail}\)相当于是删除子串的一个前缀,直接这样子明显是不行的,因为跳了\(\operatorname{fail}\)字典序没有一个很直观地表示。但是反过来考虑反串,......