我用C#写的一个Windows应用程序怎样让操作系统在启动是就启动我的程序?
3个回答
展开全部
/// <summary>
/// 保存开启启动设置的方法
/// </summary>
private void DoAutoRunXML()
{
XmlNode root = clsXMl.GetRootNode();
foreach (XmlNode node in root.ChildNodes)
{
if (node.Name == "AutoRun")
{
if (node.InnerText == "no")
{
node.InnerText = "yes";//重写XML
ToolStripMenuItemAutoRun.Checked = true;
RunWhenStart(true, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");
}
else if (node.InnerText == "yes")
{
node.InnerText = "no";
ToolStripMenuItemAutoRun.Checked = false;
RunWhenStart(false, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");
}
}
}
clsXMl.xmlSave();
}
/// <summary>
/// 修改 开机启动 注册表的方法
/// </summary>
/// <param name="bFlag"> 是否开机启动</param>
/// <param name="strName">启动值的名称</param>
/// <param name="strPath">启动程序的路径</param>
private void RunWhenStart(bool bFlag, string strName, string strPath)
{
Microsoft.Win32.RegistryKey rootKey = Microsoft.Win32.Registry.LocalMachine;//本地计算机数据的配置
Microsoft.Win32.RegistryKey runKey = rootKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (bFlag == true)//创建开机启动
{
try
{
runKey.SetValue(strName, strPath);
rootKey.Close();// 刷新 关闭 保存修改
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
runKey.DeleteValue(strName);
rootKey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
GC.Collect();
}
/// 保存开启启动设置的方法
/// </summary>
private void DoAutoRunXML()
{
XmlNode root = clsXMl.GetRootNode();
foreach (XmlNode node in root.ChildNodes)
{
if (node.Name == "AutoRun")
{
if (node.InnerText == "no")
{
node.InnerText = "yes";//重写XML
ToolStripMenuItemAutoRun.Checked = true;
RunWhenStart(true, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");
}
else if (node.InnerText == "yes")
{
node.InnerText = "no";
ToolStripMenuItemAutoRun.Checked = false;
RunWhenStart(false, Application.ProductName, Application.StartupPath + "\\" + Application.ProductName + ".exe");
}
}
}
clsXMl.xmlSave();
}
/// <summary>
/// 修改 开机启动 注册表的方法
/// </summary>
/// <param name="bFlag"> 是否开机启动</param>
/// <param name="strName">启动值的名称</param>
/// <param name="strPath">启动程序的路径</param>
private void RunWhenStart(bool bFlag, string strName, string strPath)
{
Microsoft.Win32.RegistryKey rootKey = Microsoft.Win32.Registry.LocalMachine;//本地计算机数据的配置
Microsoft.Win32.RegistryKey runKey = rootKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (bFlag == true)//创建开机启动
{
try
{
runKey.SetValue(strName, strPath);
rootKey.Close();// 刷新 关闭 保存修改
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
runKey.DeleteValue(strName);
rootKey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
GC.Collect();
}
参考资料: http://hi.baidu.com/bestgo/blog/item/bfbb8ef3d83096cf0b46e0d1.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询