c#设置程序开机启动(附源码)

上传人:第*** 文档编号:33605542 上传时间:2018-02-16 格式:DOC 页数:5 大小:104.50KB
返回 下载 相关 举报
c#设置程序开机启动(附源码)_第1页
第1页 / 共5页
c#设置程序开机启动(附源码)_第2页
第2页 / 共5页
c#设置程序开机启动(附源码)_第3页
第3页 / 共5页
c#设置程序开机启动(附源码)_第4页
第4页 / 共5页
c#设置程序开机启动(附源码)_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《c#设置程序开机启动(附源码)》由会员分享,可在线阅读,更多相关《c#设置程序开机启动(附源码)(5页珍藏版)》请在金锄头文库上搜索。

1、原理就是在注册表启动项里添加一项。路径:SOFTWAREMicrosoftWindowsCurrentVersionRun或者直接:运行-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 CSha

2、rpStartpublic partial class Form1 : Form public Form1()InitializeComponent();private void btnSet_Click(object sender, EventArgs e)SetAutoRun(D:CSharpStart.exe,true);/ / 设置应用程序开机自动运行/ / 应用程序的文件名/ 是否自动运行,为 false 时,取消自动运行/ 设置不成功时抛出异常public static void SetAutoRun(string fileName, bool isAutoRun)Registry

3、Key reg = null;tryif (!System.IO.File.Exists(fileName)throw new Exception(该文件不存在! );String name = fileName.Substring(fileName.LastIndexOf() + 1);reg = Registry.LocalMachine.OpenSubKey(SOFTWAREMicrosoftWindowsCurrentVersionRun, true);if (reg = null)reg = Registry.LocalMachine.CreateSubKey(SOFTWAREMic

4、rosoftWindowsCurrentVersionRun);if (isAutoRun)reg.SetValue(name, fileName);elsereg.SetValue(name, false);catch (Exception ex) throw new Exception(ex.ToString();finallyif (reg != null)reg.Close();/另外也可以写成服务,不过服务的话一般是在后台执行的,没有程序界面。或者直接:1. /添加启动2. RegistryKey ms_run = Registry.LocalMachine.OpenSubKey(S

5、OFTWARE/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, );开机自启public static

6、bool SetAutoRun(string keyName,string filePath)tryRegistryKey runKey=Registry.LocalMachine.OpenSubKey(SOFTWAREMicrosoftWindowsCurrentVersionRun,true);runKey.SetValue(keyName,filePath);runKey.Close();catchreturn false;return true;test: SetAutoRun(myexe,c:text.exe);另一个实例:宿舍的肥仔每次开机就需要运行多个程序,他希望一个程序实现这些

7、事情,所以写了个程序,大家有什么意见可以给我提出来,我会多加修改,谢谢,贴上源码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.IO;namespace 懒人程序public partial class Form1 : Formpublic Form1(

8、)InitializeComponent();private void Form1_Load(object sender, EventArgs e)System.Diagnostics.ProcessStartInfo info=new System.Diagnostics.ProcessStartInfo();info.UseShellExecute = true;Process p;string path = C:a.txt;FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);StreamReader s

9、r = new StreamReader(fs, Encoding.Default);string rl;while (rl = sr.ReadLine() != null)info.FileName = rl;/richTextBox1.AppendText(rl + n);p = Process.Start(info);sr.Close();fs.Close();Application.Exit();使用说明:先安装好软件,20 多 K 吧,运行前先在 c 盘根目录建立一个 a.txt,然后把需要同时运行的程序详细路径,包括程序名和 exe 后缀,一个程序占一行,可以同时有多行,例如:C:Program FilesInternet Exploreriexplore.exeC:Program FilesInternet Exploreriexplore.exe

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号