程序开机自动启动
2007-10-26 10:07
405 查看
//RunWhenStart.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
namespace Walter.K.Wang
{
/// <summary>
///
/// </summary>
public class RunWhenStart
{
/// <summary>
/// 开机自动启动程序
/// </summary>
/// <param name="Started">true为自动启动,false为不自动启动</param>
public static void Run(bool Started)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
if (Started == true)
{
try
{
Run.SetValue(Application.ProductName, Application.StartupPath + @"" + Application.ProductName + @".exe");
HKLM.Close();
}
catch (Exception Err)
{
throw new Exception(Err.Message);
}
}
else
{
try
{
Run.DeleteValue(Application.ProductName);
HKLM.Close();
}
catch (Exception Err)
{
throw new Exception(Err.Message);
}
}
}
/// <summary>
/// 检测程序是否自动启动
/// </summary>
/// <returns>自动启动为true,不自动启动为false</returns>
public static bool Getstate()
{
RegistryKey hkml = Registry.LocalMachine;
string[] aimnames;
string keyData = string.Empty;
hkml = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
aimnames = hkml.GetValueNames();
bool getin = false;
foreach (string aimKey in aimnames)
{
if (aimKey == Application.ProductName)
{
getin = true;
}
}
return getin;
}
}
}
//调用代码
if (Walter.K.Wang.RunWhenStart.Getstate() == false)
...{
Walter.K.Wang.RunWhenStart.Run(true);
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
namespace Walter.K.Wang
{
/// <summary>
///
/// </summary>
public class RunWhenStart
{
/// <summary>
/// 开机自动启动程序
/// </summary>
/// <param name="Started">true为自动启动,false为不自动启动</param>
public static void Run(bool Started)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
if (Started == true)
{
try
{
Run.SetValue(Application.ProductName, Application.StartupPath + @"" + Application.ProductName + @".exe");
HKLM.Close();
}
catch (Exception Err)
{
throw new Exception(Err.Message);
}
}
else
{
try
{
Run.DeleteValue(Application.ProductName);
HKLM.Close();
}
catch (Exception Err)
{
throw new Exception(Err.Message);
}
}
}
/// <summary>
/// 检测程序是否自动启动
/// </summary>
/// <returns>自动启动为true,不自动启动为false</returns>
public static bool Getstate()
{
RegistryKey hkml = Registry.LocalMachine;
string[] aimnames;
string keyData = string.Empty;
hkml = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
aimnames = hkml.GetValueNames();
bool getin = false;
foreach (string aimKey in aimnames)
{
if (aimKey == Application.ProductName)
{
getin = true;
}
}
return getin;
}
}
}
//调用代码
if (Walter.K.Wang.RunWhenStart.Getstate() == false)
...{
Walter.K.Wang.RunWhenStart.Run(true);
}
相关文章推荐
- VC 开机自动启动程序代码
- VC 中设置程序 开机自动启动和取消自动启动
- android 开机自动启动程序
- Qt Windows下开机自动启动自己的程序
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- Ubuntu添加开机自动启动程序方法
- [Windows][VC]开机自动启动程序的几种方法
- 使用Java Service Wrapper将java程序作为linux服务并且开机自动启动
- 在Linux系统如何让程序开机时自动启动
- 将node.js程序作为服务,并在windows下开机自动启动(使用forever)
- intel dialogic configuration manager 语音卡程序开机自动启动
- C# 开机自动启动程序
- 在linux下设置开机自动启动程序的方法[转]
- Linux开机自动启动程序
- C# 制作开机自动启动程序
- Ubuntu添加开机自动启动程序的方法
- 设置嵌入式系统开机自动启动程序
- 开机自动启动程序
- 使用Java Service Wrapper将java程序作为linux服务并且开机自动启动
- 使指定程序在开机的时候自动启动