基本信息
源码名称:C#自启动程序
源码大小:0.09M
文件格式:.zip
开发语言:C#
更新时间:2020-04-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace AutoStartAppMgr
{
    public partial class FormMain : Form
    {
        int tmrCounts;
        int autoAppNrs;
        int AppStartupNo;
        int Interval;
        Boolean bExitReady;
        List<string> lstAppInfo = new List<string>();
        public FormMain()
        {
            InitializeComponent();
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            InitData();
            ReadAppInfo();
            //StartupAllApps();
            //this.Close();
            if (!IsSetThisAppAutoStartup())
                SetThisAppAutoStartup();
            JedgeAppAutoSatus();
        }
        private void InitData()
        {
            tmrCounts = -1;
            autoAppNrs = -1;
            AppStartupNo = 0;
            Interval = 2;
            bExitReady = false;
        }
        private void JedgeAppAutoSatus()
        {
            if (IsSetThisAppAutoStartup())
            {
                but_SetAutoStartup.Enabled = false;
                but_DelAutoStartup.Enabled = true;
            }
            else
            {
                but_SetAutoStartup.Enabled = true;
                but_DelAutoStartup.Enabled = false;
            }
        }
        private Boolean IsSetThisAppAutoStartup()
        {
            string strPathAndAppName = Application.ExecutablePath;
            if (!File.Exists(strPathAndAppName))
                return false;
            string strAppName = strPathAndAppName.Substring(strPathAndAppName.LastIndexOf("\\")   1);//
            strAppName = strAppName.Replace(".exe", "");
            strAppName = strAppName.Replace(".EXE", "");
            RegistryKey local_chk = Registry.LocalMachine;
            RegistryKey run_Check = local_chk.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            //var KeyVal = run_Check.GetValue("360Safetray");//HotKeysCmds
            var KeyVal = run_Check.GetValue(strAppName);
            if (KeyVal != null)
            {
                //string tmpStr = KeyVal.ToString().ToLower();
                if (KeyVal.ToString().ToLower() !=  "false")//@"c:\Windows\system32\hkcmd.exe")
                //if (tmpStr != @"c:\Windows\system32\hkcmd.exe")
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        private void ReadAppInfo()
        {
            string strPath = @"C:\CONFIG\AutoAppsInfo.ini";
            StringBuilder MyString = new StringBuilder(256);
            try
            {
                AutoStartAppMgr.WinAPI.GetPrivateProfileString("AppGenInfo", "totAppNrs", "", MyString, 256, strPath);
                autoAppNrs = int.Parse(MyString.ToString());
                AutoStartAppMgr.WinAPI.GetPrivateProfileString("AppGenInfo", "Interval", "", MyString, 256, strPath);
                Interval = int.Parse(MyString.ToString()); 
                for (int i = 0; i < autoAppNrs; i  )
                {
                    AutoStartAppMgr.WinAPI.GetPrivateProfileString("AppInfo", "app"   (i   1).ToString(), "", MyString, 256, strPath);
                    lstAppInfo.Add(MyString.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void StartupAllApps()
        {
            foreach (string oneAppInfo in lstAppInfo)
            {
                if (oneAppInfo != "")
                {
                    System.Diagnostics.Process.Start(oneAppInfo);
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
        private void but_SetAutoStartup_Click(object sender, EventArgs e)
        {
            SetThisAppAutoStartup();
            JedgeAppAutoSatus();
        }
        private void SetThisAppAutoStartup()
        {
            try
            {
                string strPathAndAppName = Application.ExecutablePath;
                if (!File.Exists(strPathAndAppName))
                    return;
                string strAppName = strPathAndAppName.Substring(strPathAndAppName.LastIndexOf("\\")   1);//
                strAppName = strAppName.Replace(".exe", "");
                strAppName = strAppName.Replace(".EXE", "");
                RegistryKey RKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (RKey == null)
                    RKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                RKey.SetValue(strAppName, strPathAndAppName);
                //MessageBox.Show("程序设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                RKey.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void but_DelAutoStartup_Click(object sender, EventArgs e)
        {
            DelThisAppAutoStartup();
            JedgeAppAutoSatus();
        }
        private void DelThisAppAutoStartup()
        {
            try
            {
                string strPathAndAppName = Application.ExecutablePath;
                if (!File.Exists(strPathAndAppName))
                    return;
                string strAppName = strPathAndAppName.Substring(strPathAndAppName.LastIndexOf("\\")   1);//
                strAppName = strAppName.Replace(".exe", "");
                strAppName = strAppName.Replace(".EXE", "");
                RegistryKey RKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (RKey == null)
                    RKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                RKey.SetValue(strAppName, false);//取消开机运行
                RKey.DeleteSubKey(strAppName, false);//取消开机运行
                RKey.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void Tmr01_Tick(object sender, EventArgs e)
        {
            tmrCounts  ;
            if (autoAppNrs > 0) {
                if (tmrCounts % Interval == 0)
                {
                    if (lstAppInfo.Count > 0 && AppStartupNo <= autoAppNrs-1)
                    {
                        System.Diagnostics.Process.Start(lstAppInfo[AppStartupNo]);
                        AppStartupNo  ;
                    }
                    if(AppStartupNo == autoAppNrs)
                        bExitReady = true;
                }
            }
            else
                bExitReady = true;
            if (tmrCounts > 300)
            {
                bExitReady = true;
                this.Close();
            }
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!bExitReady)
                e.Cancel = true;//Can't exit.
            else
                e.Cancel = false;//Ready for exit.
        }
    }
}