公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 實現(xiàn)軟件開機自啟動(不需要管理員權(quán)限)

admin
2025年5月7日 12:14 本文熱度 222

原理簡介

本文參考C#/WPF/WinForm/程序?qū)崿F(xiàn)軟件開機自動啟動的兩種常用方法,將里面中的第一種方法做了封裝成AutoStart類,使用時直接兩三行代碼就可以搞定。
自啟動的原理是將軟件的快捷方式創(chuàng)建到計算機的自動啟動目錄下(不需要管理員權(quán)限),這種方法更加通用、限制更少。
使用方法
使用方法如下:

//快捷方式的描述、名稱的默認值是當前的進程名,自啟動默認為正常窗口,一般情況下不需要手動設(shè)置
//設(shè)置快捷方式的描述,
AutoStart.Instance.QuickDescribe = "軟件描述";
//設(shè)置快捷方式的名稱
AutoStart.Instance.QuickName = "軟件名稱";
//設(shè)置自啟動的窗口類型,后臺服務(wù)類的軟件可以設(shè)置為最小窗口
AutoStart.Instance.WindowStyle = WshWindowStyle.WshMinimizedFocus;

//快捷方式設(shè)置true時,有就忽略、沒有就創(chuàng)建,自啟動快捷方式只能存在一個
//設(shè)置開機自啟動,true 自啟動,false 不自啟動
AutoStart.Instance.SetAutoStart(SysParam.Instance.OnOff);
//設(shè)置桌面快捷方式,true 創(chuàng)建桌面快捷方式(有就跳過,沒有就創(chuàng)建),false 刪除桌面快捷方式
AutoStart.Instance.SetDesktopQuick(true);

完整代碼

引用以下命名空間:

//添加引用,在 Com 中搜索 Windows Script Host Object Model
using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

AutoStart類代碼:

public class AutoStart
{
    #region 公開

    /// <summary>
    /// 唯一實例,也可以自定義實例
    /// </summary>
    public static AutoStart Instance { getprivate set; } = new AutoStart();

    /// <summary>
    /// 快捷方式描述,默認值是當前的進程名
    /// </summary>
    public string QuickDescribe { getset; } = Process.GetCurrentProcess().ProcessName;

    /// <summary>
    /// 快捷方式名稱,默認值是當前的進程名
    /// </summary>
    public string QuickName { getset; } = Process.GetCurrentProcess().ProcessName;

    /// <summary>
    /// 自啟動窗口類型,默認值是正常窗口
    /// </summary>
    public WshWindowStyle WindowStyle { getset; } = WshWindowStyle.WshNormalFocus;

    /// <summary>
    /// 設(shè)置開機自動啟動-只需要調(diào)用改方法就可以了參數(shù)里面的bool變量是控制開機啟動的開關(guān)的,默認為開啟自啟啟動
    /// </summary>
    /// <param name="onOff">自啟開關(guān)</param>
    public void SetAutoStart(bool onOff = true)
    {
        if (onOff)//開機啟動
        {
            //獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
            List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
            //存在2個以快捷方式則保留一個快捷方式-避免重復(fù)多于
            if (shortcutPaths.Count >= 2)
            {
                for (int i = 1; i < shortcutPaths.Count; i++)
                {
                    DeleteFile(shortcutPaths[i]);
                }
            }
            else if (shortcutPaths.Count < 1)//不存在則創(chuàng)建快捷方式
            {
                CreateShortcut(systemStartPath, QuickName, appAllPath, QuickDescribe,WindowStyle);
            }
        }
        else//開機不啟動
        {
            //獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
            List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
            //存在快捷方式則遍歷全部刪除
            if (shortcutPaths.Count > 0)
            {
                for (int i = 0; i < shortcutPaths.Count; i++)
                {
                    DeleteFile(shortcutPaths[i]);
                }
            }
        }
        //創(chuàng)建桌面快捷方式-如果需要可以取消注釋
        //CreateDesktopQuick(desktopPath, QuickName, appAllPath);
    }

    /// <summary>
    /// 在桌面上創(chuàng)建快捷方式-如果需要可以調(diào)用
    /// </summary>
    public void SetDesktopQuick(bool isCreate)
    {
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appAllPath);
        if (isCreate)
        {
            //沒有就創(chuàng)建
            if (shortcutPaths.Count < 1)
            {
                CreateShortcut(desktopPath, QuickName, appAllPath, QuickDescribe, WshWindowStyle.WshNormalFocus);
            }
        }
        else
        {
            //有就刪除
            for (int i = 0; i < shortcutPaths.Count; i++)
            {
                DeleteFile(shortcutPaths[i]);
            }
        }
    }

    #endregion 公開

    #region 私有

    /// <summary>
    /// 自動獲取系統(tǒng)自動啟動目錄
    /// </summary>
    private string systemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

    /// <summary>
    /// 自動獲取程序完整路徑
    /// </summary>
    private string appAllPath = Process.GetCurrentProcess().MainModule.FileName;

    /// <summary>
    /// 自動獲取桌面目錄
    /// </summary>
    private string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    /// <summary>
    ///  向目標路徑創(chuàng)建指定文件的快捷方式
    /// </summary>
    /// <param name="directory">目標目錄</param>
    /// <param name="shortcutName">快捷方式名字</param>
    /// <param name="targetPath">文件完全路徑</param>
    /// <param name="description">描述</param>
    /// <param name="iconLocation">圖標地址</param>
    /// <returns>成功或失敗</returns>
    private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description, WshWindowStyle windowStyle, string iconLocation = null)
    {
        try
        {
            //目錄不存在則創(chuàng)建
            if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
            //合成路徑
            string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
            //存在則不創(chuàng)建
            if (System.IO.File.Exists(shortcutPath)) return true;
            //添加引用 Com 中搜索 Windows Script Host Object Model
            WshShell shell = new IWshRuntimeLibrary.WshShell();
            //創(chuàng)建快捷方式對象
            IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
            //指定目標路徑
            shortcut.TargetPath = targetPath;
            //設(shè)置起始位置
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
            //設(shè)置運行方式,默認為常規(guī)窗口
            shortcut.WindowStyle = (int)windowStyle;
            //設(shè)置備注
            shortcut.Description = description;
            //設(shè)置圖標路徑
            shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;
            //保存快捷方式
            shortcut.Save();
            return true;
        }
        catch (Exception ex)
        {
            string temp = ex.Message;
            temp = "";
        }
        return false;
    }

    /// <summary>
    /// 獲取指定文件夾下指定應(yīng)用程序的快捷方式路徑集合
    /// </summary>
    /// <param name="directory">文件夾</param>
    /// <param name="targetPath">目標應(yīng)用程序路徑</param>
    /// <returns>目標應(yīng)用程序的快捷方式</returns>
    private List<stringGetQuickFromFolder(string directory, string targetPath)
    {
        List<string> tempStrs = new List<string>();
        tempStrs.Clear();
        string tempStr = null;
        string[] files = Directory.GetFiles(directory, "*.lnk");
        if (files == null || files.Length < 1)
        {
            return tempStrs;
        }
        for (int i = 0; i < files.Length; i++)
        {
            //files[i] = string.Format("{0}\\{1}", directory, files[i]);
            tempStr = GetAppPathFromQuick(files[i]);
            if (tempStr == targetPath)
            {
                tempStrs.Add(files[i]);
            }
        }
        return tempStrs;
    }

    /// <summary>
    /// 獲取快捷方式的目標文件路徑-用于判斷是否已經(jīng)開啟了自動啟動
    /// </summary>
    /// <param name="shortcutPath"></param>
    /// <returns></returns>
    private string GetAppPathFromQuick(string shortcutPath)
    {
        //快捷方式文件的路徑 = @"d:\Test.lnk";
        if (System.IO.File.Exists(shortcutPath))
        {
            WshShell shell = new WshShell();
            IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
            //快捷方式文件指向的路徑.Text = 當前快捷方式文件IWshShortcut類.TargetPath;
            //快捷方式文件指向的目標目錄.Text = 當前快捷方式文件IWshShortcut類.WorkingDirectory;
            return shortct.TargetPath;
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 根據(jù)路徑刪除文件-用于取消自啟時從計算機自啟目錄刪除程序的快捷方式
    /// </summary>
    /// <param name="path">路徑</param>
    private void DeleteFile(string path)
    {
        FileAttributes attr = System.IO.File.GetAttributes(path);
        if (attr == FileAttributes.Directory)
        {
            Directory.Delete(path, true);
        }
        else
        {
            System.IO.File.Delete(path);
        }
    }

    #endregion 私有
}

總結(jié)

在本文中,我們探討了如何使用C#語言實現(xiàn)應(yīng)用程序在系統(tǒng)啟動時自動運行的功能,同時避免了對管理員權(quán)限的需求。通過這種方法,用戶可以在不進行額外配置的情況下,確保應(yīng)用程序隨系統(tǒng)啟動而自動加載,極大地提高了使用的便捷性和程序的可用性。


該文章在 2025/5/7 12:14:17 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 成人午夜影院在线观看 | 国产巨胸爆乳裸体免费视频 | 国产主播国内精品在线 | 91黄视频在线 | 国产精品亚洲综合日韩在线 | 国产电影一级视频在线 | 国产综合久久久久久鬼色 | 国精产品一二二线网站 | 国产不卡福利片在 | 国产日韩欧美一区二区东京热a | 国产成人v无码精品天堂 | 2025亚洲综合一区二区 | 精品久久久久久中文字幕无码漫画 | 国产精品无码专区视频 | 精品视频在线观自拍自拍 | 国产爆乳无码精品视频 | 国产毛片一级片试看 | 2025国产精品午夜久久 | 国产91精品露脸国语对白 | 国产裸体歌舞一区二区视频 | av在线观看免费播放 | 国产精品白丝av | av无码久久久久不卡网站毛 | 国产午夜在线看免费观看视频 | 国产精品无码无卡毛片不卡视 | 精品麻豆三级 | 国产精品成人va在线观看软件 | 国产精品午夜国产小视频 | 精品亚洲欧洲一区二区三区不卡 | 国产高清不卡码一区二区三区 | 精品无码久久久久久动漫 | 国产午夜av在线 | 不卡无码人妻一区三区 | 精品蜜桃秘一区二区三区粉嫩 | 国产av无码专区亚洲精品 | 东京热aⅴ无码一区二区 | 国产精品一区二区在线观看完整版 | 成人午夜亚洲精品无码网站 | 国产欧美日韩另类专区 | 精品久久久久久天堂色毛毛 | 国产福利秒拍一区二区在线观看 |