当前位置:首页 > .NET

C# 实现程序开机自启动

大滑稽8年前 (2018-02-14).NET2585
private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
   try
	{
		//设置开机自启动  
		if (checkBox8.Checked == true)
		{
			/*方法一*/
			string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
			//获得文件的当前路径
			string dir = Directory.GetCurrentDirectory();
			//获取可执行文件的全部路径
			string exeDir = dir + @"\自动备份.exe.lnk";
			File.Copy(exeDir, StartupPath + @"\自动备份.exe.lnk", true);
			/*方法二*/
			string path = Application.ExecutablePath;
			RegistryKey rk = Registry.LocalMachine;
			RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
			rk2.SetValue("JcShutdown", path);
			rk2.Close();
			rk.Close();
		}
		//取消开机自启动  
		else
		{
			/*方法一*/
			string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup);
			System.IO.File.Delete(StartupPath + @"\EcgNetPlug.exe.lnk");
			/*方法二*/
			string path = Application.ExecutablePath;
			RegistryKey rk = Registry.LocalMachine;
			RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
			rk2.DeleteValue("JcShutdown", false);
			rk2.Close();
			rk.Close();
		}
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
	}
}


扫描二维码推送至手机访问。

版权声明:本文由第★一★次发布,如需转载请注明出处。

本文链接:https://blog.wpers.net/post/5.html

分享给朋友:

“C# 实现程序开机自启动” 的相关文章

C#遍历控件的方法

首先,要想遍历,就必须找到你想找的表单里面的所有控件,然后一个个的逐一比对,当找到了你需要的控件的时候,再做你需要的操作。1、foreach方法foreach (Control control in …

修改注册表限制软件使用次数

private void Form1_Load(object sender, System.EventArgs e){RegistryKey RootKey,RegKey; //项名为:HKEY_CURRENT_USER\Software…

c#中分页显示数据

     //c#中分页显示数据    public partial class Form1 : Form    {  …

c#各种Timer类的区别与使用

System.Threading.Timer  是一个简单的轻量计时器,它使用回调方法并由线程池线程提供服务。在必须更新用户界面的情况下,建议不要使用该计时器,因为它的回调不在用户界面线程上发生。在此类情况下,System.Win…

C#程序防止重复运行

 Boolean mutexWasCreated;//声明一个Boolean值,用于下面的Out//true 为是否给予当前这个线程互斥的功能, true为是, false为否,也就是说是否不允许两个相同名称的线程存在//可以…

HttpWebRequest验证证书文件获取网页内容

         /// 传入URL返回网页的html代码        pu…