通过注册表完成开机自动启动

 

/// <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();            }        }

 

代码
/// <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();
}

}
代码
通过注册表完成开机自动启动。
static public string RegeditPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\";

/// <summary>
/// 读注册表数据信息
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetRegistData(string name) //键名称
{
string registData;
RegistryKey hkml
= Registry.LocalMachine;
RegistryKey software
= hkml.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir
= software.OpenSubKey(ReigstidFileName, true);
registData
= aimdir.GetValue(name).ToString();
return registData;
}
/// <summary>
/// 写注册表数据信息,分别是子键名称和值存放LocalMachine目录下的XXX文件中,其文件名为name的值,值为tovalue的值

/// </summary>
/// <param name="name"></param>
/// <param name="tovalue"></param>
/// <returns></returns>
public bool WTRegedit(string name, string tovalue)
{
bool WriteOk = false;
try
{
RegistryKey hklm
= Registry.LocalMachine;
RegistryKey software
= hklm.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir
= software.CreateSubKey(ReigstidFileName);
aimdir.SetValue(name, tovalue);
WriteOk
= true;
}
catch
{
WriteOk
= false;
}
return WriteOk;
}
/// <summary>
///
///修改注册表数据信息
/// </summary>
/// <param name="name"></param>
/// <param name="tovalue"></param>
/// <returns></returns>
public bool EditRegedit(string name, string tovalue)
{
bool EditOk = false;
try
{
if (IsRegeditExist(name))
{
RegistryKey hklm
= Registry.LocalMachine;
RegistryKey software
= hklm.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir
= software.CreateSubKey(ReigstidFileName);
aimdir.SetValue(name, tovalue);
EditOk
= true;
}
}
catch
{
EditOk
= false;
}
return EditOk;
}

/// <summary>
/// 删除注册表数据信息
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool DeleteRegist(string name)
{
bool DeleteOk = false;
string[] aimnames;
RegistryKey hkml
= Registry.LocalMachine;
RegistryKey software
= hkml.OpenSubKey(RegeditPath, true);
RegistryKey aimdir
= software.OpenSubKey("Run", true);
aimnames
= aimdir.GetValueNames();
foreach (string aimKey in aimnames)
{
if (aimKey == name)
{
aimdir.Deletue(name);
DeleteOk
= true;
}
}
return DeleteOk;
}
/// <summary>
/// 判断注册表数据信息是否存在
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool IsRegeditExist(string name)
{
bool isExist = false;
string[] subkeyNames;
RegistryKey hkml
= Registry.LocalMachine;
RegistryKey software
= hkml.OpenSubKey(RegeditPath, true);
RegistryKey aimdir
= software.OpenSubKey("Run");
subkeyNames
= aimdir.GetValueNames();
foreach (string keyName in subkeyNames)
{
if (keyName == name)
{
isExist
= true;
}
}
return isExist;
}

 

posted @ 2010-12-20 21:47  sweetjian  阅读(515)  评论(0编辑  收藏  举报