国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C#程序開機自啟

這篇具有很好參考價值的文章主要介紹了C#程序開機自啟。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

第一種方式 修改注冊表

  /// <summary>
        /// 開機啟動
        /// </summary>
        public void OpenStart()
        {
            //獲得程序路徑
            var starupPath = GetType().Assembly.Location;
            try
            {
                var fileName = starupPath;
                //設(shè)置啟動項的key 可以改
                var shortFileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
                //打開子鍵節(jié)點
                var myReg = Registry.LocalMachine.OpenSubKey(
                    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree,
                    RegistryRights.FullControl);
                if (myReg == null)
                {
                    //如果子鍵節(jié)點不存在,則創(chuàng)建之
                    myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                if (myReg != null && myReg.GetValue(shortFileName) != null)
                {
                    //在注冊表中設(shè)置自啟動程序
                    myReg.DeleteValue(shortFileName);
                    myReg.SetValue(shortFileName, fileName);
                }
                else if (myReg != null && myReg.GetValue(shortFileName) == null)
                {
                    //設(shè)置啟動項的key 和啟動路徑*******
                    myReg.SetValue(shortFileName, fileName);
                }
            }
            catch
            {

            }
        }

 /// <summary>
        /// 判斷注冊鍵值對是否存在,即是否處于開機啟動狀態(tài)
        /// </summary>
        /// <param name="keyName">鍵值名</param>
        /// <returns></returns>
        private static bool IsExistKey(string keyName)
        {
            try
            {
                bool _exist = false;
                RegistryKey local = Registry.LocalMachine;
                RegistryKey runs = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (runs == null)
                {
                    RegistryKey key2 = local.CreateSubKey("SOFTWARE");
                    RegistryKey key3 = key2.CreateSubKey("Microsoft");
                    RegistryKey key4 = key3.CreateSubKey("Windows");
                    RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
                    RegistryKey key6 = key5.CreateSubKey("Run");
                    runs = key6;
                }
                string[] runsName = runs.GetValueNames();
                foreach (string strName in runsName)
                {
                    if (strName.ToUpper() == keyName.ToUpper())
                    {
                        _exist = true;
                        return _exist;
                    }
                }
                return _exist;

            }
            catch
            {
                return false;
            }
        }
 /// <summary>
        /// 取消開機啟動
        /// </summary>
        /// <param name="key"></param>
        private void DelStart(string key)
        {
            try
            {
                string RegeditPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\";
                RegistryKey rgk = Registry.LocalMachine
                .OpenSubKey(RegeditPath, true)
                .OpenSubKey("Run", true);
                string[] str = rgk.GetValueNames();
                rgk.DeleteValue(key);
                rgk.Close();
            }
            catch (Exception ex)
            {
               
            }
        }

第二種添加快捷方式到啟動菜單中

添加引用,在 Com 中搜索 Windows Script Host Object Mod文章來源地址http://www.zghlxwxcb.cn/news/detail-771521.html

c#開機自啟動代碼,c#,microsoft,開發(fā)語言,Powered by 金山文檔
 using IWshRuntimeLibrary;  //添加引用,在 Com 中搜索 Windows Script Host Object Model
using System.Diagnostics;

/// <summary>
        /// 快捷方式名稱-任意自定義
        /// </summary>
        private const string QuickName = "WindowsClient";

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

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

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

        /// <summary>
        /// 設(shè)置開機自動啟動-只需要調(diào)用改方法就可以了參數(shù)里面的bool變量是控制開機啟動的開關(guān)的,默認(rèn)為開啟自啟啟動
        /// </summary>
        /// <param name="onOff">自啟開關(guān)</param>
        /// <param name="appPath">要設(shè)置自動啟動的路徑</param>
        public void SetMeAutoStart(bool onOff = true, string appPath = null)
        {
            if (onOff)//開機啟動
            {
                //獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
                List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appPath != null ? appPath : 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, appPath != null ? appPath : appAllPath, "SAGA");
                }
            }
            else//開機不啟動
            {
                //獲取啟動路徑應(yīng)用程序快捷方式的路徑集合
                List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appPath != null ? appPath : appAllPath);
                //存在快捷方式則遍歷全部刪除
                if (shortcutPaths.Count > 0)
                {
                    for (int i = 0; i < shortcutPaths.Count; i++)
                    {
                        DeleteFile(shortcutPaths[i]);
                    }
                }
            }
            //創(chuàng)建桌面快捷方式-如果需要可以取消注釋
            //CreateDesktopQuick(desktopPath, QuickName, appAllPath);
        }

        /// <summary>
        ///  向目標(biāo)路徑創(chuàng)建指定文件的快捷方式
        /// </summary>
        /// <param name="directory">目標(biāo)目錄</param>
        /// <param name="shortcutName">快捷方式名字</param>
        /// <param name="targetPath">文件完全路徑</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">圖標(biāo)地址</param>
        /// <returns>成功或失敗</returns>
        private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
        {
            try
            {
                //啟動項菜單
                //C:\Users\lenovo\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
                if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);                         //目錄不存在則創(chuàng)建
                //添加引用 Com 中搜索 Windows Script Host Object Model
                string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));          //合成路徑
                WshShell shell = new IWshRuntimeLibrary.WshShell();
                IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);    //創(chuàng)建快捷方式對象
                shortcut.TargetPath = targetPath;                                                               //指定目標(biāo)路徑
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);                                  //設(shè)置起始位置
                shortcut.WindowStyle = 1;                                                                       //設(shè)置運行方式,默認(rèn)為常規(guī)窗口
                shortcut.Description = description;                                                             //設(shè)置備注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;    //設(shè)置圖標(biāo)路徑
                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">目標(biāo)應(yīng)用程序路徑</param>
        /// <returns>目標(biāo)應(yīng)用程序的快捷方式</returns>
        private List<string> GetQuickFromFolder(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>
        /// 獲取快捷方式的目標(biāo)文件路徑-用于判斷是否已經(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 = 當(dāng)前快捷方式文件IWshShortcut類.TargetPath;
                //快捷方式文件指向的目標(biāo)目錄.Text = 當(dāng)前快捷方式文件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);
            }
        }

到了這里,關(guān)于C#程序開機自啟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • java項目(jar包)配置為開機自啟 jar包bat腳本啟動和配置為開機自啟

    java項目(jar包)配置為開機自啟 jar包bat腳本啟動和配置為開機自啟

    今天給大家分享一下,如何把java項目,打包--------打包后啟動---------生成bat文件啟動-----------設(shè)置為開機自啟。 每一步都是先是文字描述,后面是圖片展示。 首先是打包,咱們寫好的項目在maven處 點擊clear,等待完成后,點擊package ???????? 打包完成后,在target目錄下可以

    2024年02月09日
    瀏覽(24)
  • MySQL服務(wù)關(guān)閉開機自啟,改成手動啟動狀態(tài)

    MySQL服務(wù)關(guān)閉開機自啟,改成手動啟動狀態(tài)

    最近在寫前端,所以就先把后端數(shù)據(jù)庫禁用或手動啟動吧。防止浪費太多內(nèi)存或資源。一般就之前損壞了的數(shù)據(jù)庫就禁用吧,其他最近不常用的服務(wù)就沒必要開機自啟動吧,畢竟電腦只有一臺,不想學(xué)習(xí)想用電腦來玩的話就講工作服務(wù)從開機自啟動狀態(tài)改成手動狀態(tài)吧。 首先

    2024年02月09日
    瀏覽(23)
  • Python實現(xiàn)開機自啟代碼及取消(通過修改注冊表)

    話不多說,先上代碼 基于sys、os、winreg模塊實現(xiàn)的程序開機自啟,其中 add_to_startup() 函數(shù)用于添加自啟動項, remove_from_startup() 函數(shù)用于刪除啟動項 無需自行安裝額外模塊 自帶模塊:sys、os、winreg name(str) :添加到注冊表中的鍵值,需要具有一定標(biāo)識性、獨特性,避免與其他

    2024年02月03日
    瀏覽(20)
  • C# Winform程序跟隨系統(tǒng)啟動

    C# Winform程序跟隨系統(tǒng)啟動

    ? ?說明:有時候Winform程序,寫完了,安裝在window服務(wù)器上,需要每天重啟服務(wù)器(擔(dān)心程序運行舊了就會卡),每次重啟后又擔(dān)心程序沒啟動,這篇文章可以解決這個問題 Window 10系統(tǒng)測試 設(shè)備名稱?? ?i7BooK 處理器?? ?Intel(R) Core(TM) i7-6660U CPU @ 2.40GHz ? 2.40 GHz 機帶 RAM?? ?8

    2023年04月09日
    瀏覽(20)
  • 【C#】【winform】Microsoft Visual Studio Installer Project 打包應(yīng)用程序全部過程

    【C#】【winform】Microsoft Visual Studio Installer Project 打包應(yīng)用程序全部過程

    提示:只針對擴展包來完成打包的工作過程。 在做完C#和winform的開發(fā),完成之后,需要做一些打包的工具,在這個過程中遇到一些問題,所以記錄下我的操作過程和遇到的異常情況。 支持快速打包的插件擴展 在擴展—搜搜 Microsoft Visual Studio Installer Project,安裝,然后等待下

    2024年02月07日
    瀏覽(31)
  • Android APP開機啟動,安卓APP開發(fā)自啟動,安卓啟動后APP自動啟動 Android讓程序開機自動運行APP

    Android APP開機啟動,安卓APP開發(fā)自啟動,安卓啟動后APP自動啟動 Android讓程序開機自動運行APP

    第一步設(shè)置獲取廣播后的業(yè)務(wù) 第二查權(quán)限給APP 理論以上兩步做完就可以了。APP也能收到廣播信息了, 但是APP沒有在桌面啟動。 經(jīng)過再研究,發(fā)現(xiàn)要在手機再設(shè)置自動開啟等業(yè)務(wù),以下是小米、魅族的系統(tǒng)設(shè)置的一些內(nèi)容,其它平臺自己研究。 這里已經(jīng)顯示收到廣播信息 ?

    2024年02月06日
    瀏覽(40)
  • 【C#】獲取已安裝的應(yīng)用名稱、啟動路徑、安裝位置、產(chǎn)品代碼、卸載字符串等

    代碼 例子 參考 c#獲取系統(tǒng)已安裝軟件列表(32,64位均可使用)_zhoyuwo的博客-CSDN博客

    2024年02月13日
    瀏覽(17)
  • C#程序的啟動顯示方案(無窗口進(jìn)程發(fā)送消息) - 開源研究系列文章

    C#程序的啟動顯示方案(無窗口進(jìn)程發(fā)送消息) - 開源研究系列文章

    今天繼續(xù)研究C#的WinForm的實例顯示效果。 我們上次介紹了Winform窗體的唯一實例運行代碼(見博文:基于C#的應(yīng)用程序單例唯一運行的完美解決方案 - 開源研究系列文章 ?)。這就有一個問題,程序已經(jīng)打開了,這時候再次運行該應(yīng)用程序,我們的方案是將該應(yīng)用的主窗體顯示出

    2024年02月14日
    瀏覽(22)
  • asp.net文檔管理系統(tǒng)VS開發(fā)sqlserver數(shù)據(jù)庫web結(jié)構(gòu)c#編程Microsoft Visual Studio

    asp.net文檔管理系統(tǒng)VS開發(fā)sqlserver數(shù)據(jù)庫web結(jié)構(gòu)c#編程Microsoft Visual Studio

    一、源碼特點 ? ? ? ? asp.net文檔管理系統(tǒng)是一套完善的web設(shè)計管理系統(tǒng),系統(tǒng)具有完整的源代碼和數(shù)據(jù)庫,系統(tǒng)主要采用B/S模式開發(fā)。開發(fā)環(huán)境為vs2010,數(shù)據(jù)庫為sqlserver2008,使用c#語言開發(fā) asp.net文檔管理系統(tǒng) 二、功能介紹 (1)用戶管理:對用戶信息進(jìn)行添加、刪除、修改和

    2024年02月08日
    瀏覽(19)
  • asp.net歸宿管理系統(tǒng)VS開發(fā)sqlserver數(shù)據(jù)庫web結(jié)構(gòu)c#編程Microsoft Visual Studio

    asp.net歸宿管理系統(tǒng)VS開發(fā)sqlserver數(shù)據(jù)庫web結(jié)構(gòu)c#編程Microsoft Visual Studio

    一、源碼特點 ? ? ? ? asp.net歸宿管理系統(tǒng) 是一套完善的web設(shè)計管理系統(tǒng),系統(tǒng)具有完整的源代碼和數(shù)據(jù)庫,系統(tǒng)主要采用B/S模式開發(fā)。開發(fā)環(huán)境為vs2010,數(shù)據(jù)庫為sqlserver2008,使用c#語言開發(fā) asp.net歸宿管理系統(tǒng)VS開發(fā)sqlserver數(shù)據(jù)庫w 二、功能介紹 一、定時打卡(采用RFID卡)

    2024年02月09日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包