C# winform程序实现开机自启动并且识别是开机启动还是双击启动

这篇文章主要介绍了C# winform程序实现开机自启动并且识别是开机启动还是双击启动的实现代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

开机启动程序,在很多场合都会用到,尤其是那种在后台运行的程序。

效果图:


using System;
using Microsoft.Win32;
namespace AutoStartRun
{
  public sealed class SystemHelper
  {
    private SystemHelper() { }
    /// <summary>
    /// 设置程序开机启动
    /// </summary>
    /// <param name="strAppPath">应用程序exe所在文件夹</param>
    /// <param name="strAppName">应用程序exe名称</param>
    /// <param name="bIsAutoRun">自动运行状态</param>
    public static void SetAutoRun(string strAppPath, string strAppName, bool bIsAutoRun)
    {
      try
      {
        if (string.IsNullOrWhiteSpace(strAppPath)
          || string.IsNullOrWhiteSpace(strAppName))
        {
          throw new Exception("应用程序路径或名称为空!");
        }
        RegistryKey reg = Registry.LocalMachine;
        RegistryKey run = reg.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
        if (bIsAutoRun)
        {
          run.SetValue(strAppName, strAppPath);
        }
        else
        {
          if (null != run.GetValue(strAppName))
          {
            run.DeleteValue(strAppName);
          }
        }
        run.Close();
        reg.Close();
      }
      catch (Exception ex)
      {
        throw new Exception(ex.Message, ex);
      }
    }
    /// <summary>
    /// 判断是否开机启动
    /// </summary>
    /// <param name="strAppPath">应用程序路径</param>
    /// <param name="strAppName">应用程序名称</param>
    /// <returns></returns>
    public static bool IsAutoRun(string strAppPath, string strAppName)
    {
      try
      {
        RegistryKey reg = Registry.LocalMachine;
        RegistryKey software = reg.OpenSubKey(@"SOFTWARE");
        RegistryKey run = reg.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
        object key = run.GetValue(strAppName);
        software.Close();
        run.Close();
        if (null == key || !strAppPath.Equals(key.ToString()))
        {
          return false;
        }
        return true;
      }
      catch (Exception ex)
      {
        throw new Exception(ex.Message, ex);
      }
    }
  }
}

 调用方法:


 /// <summary>
    /// 设置程序开机自启动
    /// </summary>
    private void SetAutoRun()
    {
      string strFilePath = Application.ExecutablePath;
      string strFileName = System.IO.Path.GetFileName(strFilePath);
      try
      {
        SystemHelper.SetAutoRun(strFilePath + " -autostart", strFileName, !menuAutoRun.Checked);
        menuAutoRun.Checked = !menuAutoRun.Checked;
      }
      catch (Exception ex)
      {
        MessageBox.Show(this, ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }    

设置开机启动就是如此简单。

【开机启动并运行】

 那这个就不用说了,将命令行参数和开机注册表操作结合起来就可以了。

示例代码:       


/// <summary>
    /// 检查是否开机启动,并设置控件状态
    /// </summary>
    private void CheckAutoRun()
    { 
      string strFilePath = Application.ExecutablePath;
      string strFileName = System.IO.Path.GetFileName(strFilePath);
      if (SystemHelper.IsAutoRun(strFilePath + " -autostart", strFileName))
      {
        menuAutoRun.Checked = true;
      }
      else
      {
        menuAutoRun.Checked = false;
      }
    }
    private void AutoRun()
    {
      if (menuAutoRun.Checked)
      {
        string[] strArgs = Environment.GetCommandLineArgs();
        if (strArgs.Length >= 2 && strArgs[1].Equals("-autorun"))
        {
          labText.Text = "我是开机自启动运行...";
        }
      }
    }

总结

以上所述是小编给大家介绍的C# winform程序实现开机自启动并且识别是开机启动还是双击启动,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!

本文标题为:C# winform程序实现开机自启动并且识别是开机启动还是双击启动