资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
原理就是在注册表启动项里添加一项。路径: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 CSharpStartpublic 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)RegistryKey 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(SOFTWAREMicrosoftWindowsCurrentVersionRun);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(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, );开机自启public static 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);另一个实例:宿舍的肥仔每次开机就需要运行多个程序,他希望一个程序实现这些事情,所以写了个程序,大家有什么意见可以给我提出来,我会多加修改,谢谢,贴上源码: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()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 sr = 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
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号