原理就是在注册表启动项里添加一项。
路径:SOFTWARE\Microsoft\Windows\CurrentVersion\Run
或者直接:运行->regedit找到这个路径添加一项。

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 Microsoft.Win32;

namespace CSharpStart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void btnSet_Click(object sender, EventArgs e)
        {
            SetAutoRun(@"D:\CSharpStart.exe",true);
        }
        /// <summary>
        /// 设置应用程序开机自动运行
        /// </summary>
        /// <param name="fileName">应用程序的文件名</param>
        /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
        /// <exception cref="System.Exception">设置不成功时抛出异常</exception>
        public static void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }

}
        //另外也可以写成服务,不过服务的话一般是在后台执行的,没有程序界面。

}
}

url:http://greatverve.cnblogs.com/archive/2012/02/18/csharp-autorun.html
参考:

C# winform程序设置开机启动,当读取配置文件,或者加载图片如果设置的是相对路径时,开机启动时会出现问题(直接运程程序是没问题的)。这是因为开机启动的程序要使用绝对路径,相对路径不行。我们可以通过Application .StartupPath属性经过处理得到文件的绝对路径问题就解决了。

C# 通过读写注册表来设置开机启动想方法很简单,网上很多:

/// <summary>         
/// 开机启动项        
/// </summary>       
/// <param name="Started">是否启动</param>         
/// <param name="name">启动值的名称</param>          
/// <param name="path">启动程序的路径</param>         
public void RunWhenStart(bool Started, string name, string path)
{
    RegistryKey HKLM = Registry.LocalMachine;
    RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
    if (Started == true)
    {
        try
        {
            Run.SetValue(name, path);
            HKLM.Close();
        }
        catch//没有权限会异常            
        { }
    }
    else
    {
        try
        {
            Run.DeleteValue(name);
            HKLM.Close();
        }
        catch//没有权限会异常 
        { }
    }
}

或者直接:

  1. //添加启动
  2. RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
  3. ms_run.SetValue("mistysoft", Application.ExecutablePath.ToString());
  4. //删除启动(设为控,注册表项还在)
  5. RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
  6. ms_run.SetValue("mistysoft", "");
 

[转载]C#设置开机启动的更多相关文章

  1. ubuntu下设置开机启动服务

    原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...

  2. windows设置开机启动项

    一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...

  3. centos7安装docker并设置开机启动

    版本要求:查看内核版本,需大于3.10 [root@localhost ~]# uname -r -.el7.x86_64 更新内核:如果是生产机器务必慎重更新内核,避免出现不必要的问题. sudo ...

  4. openerp 7 在ubuntu上设置开机启动

    我们要让openerp开机运行起来. 第一步,先进入系统目录: cd /etc/init.d 第二步,创建文件.命名为openerp-server sudo vi openepr-server 第三步 ...

  5. Fedora 21 设置开机启动脚本

    sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...

  6. windows下架设SVN服务器并设置开机启动

    原文:windows下架设SVN服务器并设置开机启动 1.安装SVN服务器,到http://subversion.apache.org/packages.html上下载windows版的SVN,并安装 ...

  7. Linux Redis自动启动,Redis开机启动,Linux Redis设置开机启动

    Linux Redis自动启动,Redis开机启动,Linux Redis设置开机启动 >>>>>>>>>>>>>> ...

  8. Redis 设置开机启动

    1. 将下列代码保存为文件redis, 置于 /etc/init.d 下面 ########################### # chkconfig: 2345 90 10 redis服务必须在 ...

  9. ubuntu-18.04 设置开机启动脚本-亲测有效

    ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用. 2.将下列内容复制进rc-local.servi ...

随机推荐

  1. Redis 命令 - Keys

    DEL key [key ...] Delete a key 127.0.0.1:6379> SET foo hello OK 127.0.0.1:6379> DEL foo hello ...

  2. JqGrid 使用方法详解

    JQGrid JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面 ...

  3. Transaction Script模式

    Transcation Script模式适合于小项目,维护量小的项目. 好比cs文件中有一个主方法,调用了本文件中的其他方法,如果说不需要怎么维护的话Tranacation Script模式就可以了, ...

  4. maven安装配置(myeclipse)(一)

    欢迎转载:http://www.cnblogs.com/shizhongtao/p/3374130.html 对于我来说,maven主要用于jar包的管理,避免项目中频繁更换jar的版本,以及网上搜索 ...

  5. c#学习汇总-----------------多态

    刚开通了博客园的博客,原因是我的师兄和前辈们在这里写的一些学习笔记让我受益匪浅,所以决定从今天起用这个平台来记录我的点滴学习心得.我喜欢GIS二次开发,以后应该也不会脱离于此,对于编程我积累的知识太零 ...

  6. Winform ComboBox控件高亮显示

    //重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句 this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawF ...

  7. Building microservices with Spring Cloud and Netflix OSS, part 2

    In Part 1 we used core components in Spring Cloud and Netflix OSS, i.e. Eureka, Ribbon and Zuul, to ...

  8. call callb callp区别

    对于刚刚接触ILE模式开发的初级菜鸟而言,想要搞清楚这三者的区别还是有点难度的.网上虽然一些帖子对这三者进行了比较,但是这些帖子或是语焉不详,或是高度概括.对于老鸟来说或许已经足矣,但是对于初级菜鸟而 ...

  9. c#中sqlhelper类的编写(一)

    在.net平台的项目开发中,凡是用到数据库交互的,都有必要了解SqlHelper类的原理. 步骤一: 我就拿WPF项目开发作为例子.首先要新建一个App.config(应用程序配置文件).注意,在VS ...

  10. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...