[转载]C#设置开机启动
原理就是在注册表启动项里添加一项。
路径:SOFTWARE\Microsoft\Windows\CurrentVersion\Run
或者直接:运行->regedit找到这个路径添加一项。

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>
/// <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//没有权限会异常
{ }
}
}

或者直接:
- //添加启动
- RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
- ms_run.SetValue("mistysoft", Application.ExecutablePath.ToString());
- //删除启动(设为控,注册表项还在)
- RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
- ms_run.SetValue("mistysoft", "");
[转载]C#设置开机启动的更多相关文章
- ubuntu下设置开机启动服务
原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...
- windows设置开机启动项
一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...
- centos7安装docker并设置开机启动
版本要求:查看内核版本,需大于3.10 [root@localhost ~]# uname -r -.el7.x86_64 更新内核:如果是生产机器务必慎重更新内核,避免出现不必要的问题. sudo ...
- openerp 7 在ubuntu上设置开机启动
我们要让openerp开机运行起来. 第一步,先进入系统目录: cd /etc/init.d 第二步,创建文件.命名为openerp-server sudo vi openepr-server 第三步 ...
- Fedora 21 设置开机启动脚本
sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...
- windows下架设SVN服务器并设置开机启动
原文:windows下架设SVN服务器并设置开机启动 1.安装SVN服务器,到http://subversion.apache.org/packages.html上下载windows版的SVN,并安装 ...
- Linux Redis自动启动,Redis开机启动,Linux Redis设置开机启动
Linux Redis自动启动,Redis开机启动,Linux Redis设置开机启动 >>>>>>>>>>>>>> ...
- Redis 设置开机启动
1. 将下列代码保存为文件redis, 置于 /etc/init.d 下面 ########################### # chkconfig: 2345 90 10 redis服务必须在 ...
- ubuntu-18.04 设置开机启动脚本-亲测有效
ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用. 2.将下列内容复制进rc-local.servi ...
随机推荐
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Animated App Boot Example : Fastest animation at app boot time
This iPhone app shows how to create an animation that is displayed when the app starts. The animatio ...
- Oracle表空间操作
-- 查看表空间 SELECT tablespace_name, file_id, file_name, ), )||'M' total_space FROM dba_data_files ORDER ...
- Windows7下安装搭建redis教程和配置详解
作者:Sungeek 出处:http://www.cnblogs.com/Sungeek/ 欢迎转载,也请保留这段声明.谢谢! 简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于 ...
- <%@include和<jsp:include
博客地址:http://www.cnblogs.com/shizhongtao/p/3506742.html欢迎交流 <%@ include %>是编译时包含,<jsp:includ ...
- 'WinMain' : function cannot be overloaded
Create a MFC Application (UNICODE), paste following code in one of your cpp file. int APIENTRY WinMa ...
- .NET清楚Cookies
foreach (string cookiename in Request.Cookies.AllKeys) { HttpCookie cookies = Request.Cookies[cookie ...
- jQuery学习-----(一)JQuery的'$'符号用法
1.jQuery的三种$() 1)$()可以是$(expresion),即css选择器.Xpath或html元素,也就是通过上述表达式来匹配目标元素. 比如:$("a")构造的 ...
- 操作xml文档的常用方式
1.操作XML文档的两种常用方式: 1)使用XmlReader类和XmlWriter类操作 XmlReader是基于数据流的,占用极少的内存,是只读方式的,所以速度极快.只能采用遍历的模式查找数据节点 ...
- PHP发起get post put delete请求
<?php class commonFunction{ function callInterfaceCommon($URL,$type,$params,$headers){ $ch = curl ...