我想把我写的c#程序随windows启动(开机启动), 我该怎么写呢?最好给源代码,
关注!
C# code public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true); runKey.SetValue(keyName,filePath); runKey.Close(); } catch { return false; } return true; }
test: SetAutoRun("myexe","c:\\text.exe");
加上 using Microsoft.Win32;
用服务是最好的了,添加windows服务。这样不登陆也可以启动程序呀。打包时执行服务安装类(添加组件ServiceProcessInstaller、ServiceInstaller)就可以了,设置一下就可以了。
也可以在windows任务计划里面建一个任务,把exe的路径指定成程序路径就行了
学习中
mark
C# code using Microsoft.Win32; public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true); runKey.SetValue(keyName,filePath); runKey.Close(); } catch { return false; } return true; } test: SetAutoRun("myexe","c:\\text.exe");另外也可以写成服务,不过服务的话一般是在后台执行的,没有程序界面。
没看明白
就是在\SOFTWARE\Microsoft\Windows\CurrentVersion\Run下新建一项,指向你的程序路径即可。
写注册表这个最方便啦。
太感谢了。。
顶了 ,也可以在打包好的程序里拖到开始–程序—启动 然后阻碍注册表里修改下属性就行了
加进启动项就可以了呀,不一定要改注册表的!
顶!可用通过注册表完成开机自动启动。 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 = http://topic.csdn.net/u/20080112/23/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; }
学习了!
学习了.
我已经用最简单的方法解决了,以下是代码: /// <summary> /// 确定 按钮 事件(是否设置为开机自动启动) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button4_Click(object sender, EventArgs e) { if (autoCheck.Checked == true) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.SetValue("qidong", starupPath); /// MessageBox.Show("已启用开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // MessageBox.Show("没有选中"); //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.DeleteValue("qidong"); MessageBox.Show("已停止开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
发布打包程序的时候拖个程序的快捷方式到启动项里就完成了
写windows服务
最简单做法是把程序的快捷方式放到windous启动项目里面,这样windows会自己启动它
不错,不错,我去试试,呵呵……
C# code
/// <summary> /// 设置程序开机启动 /// 或取消开机启动 /// </summary> /// <param name="started">设置开机启动,或者取消开机启动</param> /// <param name="exeName">注册表中程序的名字</param> /// <param name="path">开机启动的程序路径</param> /// <returns>开启或则停用是否成功</returns> public static bool runWhenStart(bool started, string exeName, string path) { RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 if (key == null)//如果该项不存在的话,则创建该子项 { key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } if (started == true) { try { key.SetValue(exeName, path);//设置为开机启动 key.Close(); } catch { return false; } } else { try { key.DeleteValue(exeName);//取消开机启动 key.Close(); } catch { return false; } } return true; }
http://download.csdn.net/source/1972131
不错,收藏了!
感谢楼上的好心人 学习了
学习学习!
C# code public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWA……顶了…
不错,以后会用上,打个标记,以后来看。
我觉的你用程序生成一个快捷方式,然后复制到系统的启动项里,这样比较好一点。我以前做过一个自启动的程序,先写注册表启动,但是发现,每次通过注册表启动的,就不能通过webservice访问网络,后来没办法,直接加到启动项里就可以访问网络了。我想这可能与启动顺序有关系。
mark!
支持一个!
学习中!
关注!+1
关注!~~~~
楼上好多了兄弟都给解答了 看来来晚了
用代码 写成服务怎么去写啊! 和多人希望高手去以服务那种形式, 写注册表 未免也带容易了吧!!
mark…….
Mark了,我也来学习学习
牛人多多
16楼正解,2楼不可行。
19楼正解,2楼不行。差点高错。误导其他人
16.,19都行,临机实验过
评论功能因故关闭!
请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)
Copyright © 2007-2010 www.Chengxy.com All rights reserved
Powered by 王牌程序员
关注!
C# code
public static bool SetAutoRun(string keyName,string filePath)
{
try
{
RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
runKey.SetValue(keyName,filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
test: SetAutoRun("myexe","c:\\text.exe");
加上 using Microsoft.Win32;
用服务是最好的了,添加windows服务。这样不登陆也可以启动程序呀。打包时执行服务安装类(添加组件ServiceProcessInstaller、ServiceInstaller)就可以了,设置一下就可以了。
也可以在windows任务计划里面建一个任务,把exe的路径指定成程序路径就行了
学习中
mark
C# code
using Microsoft.Win32;
public static bool SetAutoRun(string keyName,string filePath)
{
try
{
RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
runKey.SetValue(keyName,filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
test: SetAutoRun("myexe","c:\\text.exe");另外也可以写成服务,不过服务的话一般是在后台执行的,没有程序界面。
没看明白
就是在\SOFTWARE\Microsoft\Windows\CurrentVersion\Run下新建一项,指向你的程序路径即可。
写注册表这个最方便啦。
写注册表这个最方便啦。
太感谢了。。
顶了 ,也可以在打包好的程序里拖到开始–程序—启动 然后阻碍注册表里修改下属性就行了
加进启动项就可以了呀,不一定要改注册表的!
顶!可用通过注册表完成开机自动启动。 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 = http://topic.csdn.net/u/20080112/23/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; }
学习了!
学习了.
我已经用最简单的方法解决了,以下是代码: /// <summary> /// 确定 按钮 事件(是否设置为开机自动启动) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button4_Click(object sender, EventArgs e) { if (autoCheck.Checked == true) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.SetValue("qidong", starupPath); /// MessageBox.Show("已启用开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // MessageBox.Show("没有选中"); //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.DeleteValue("qidong"); MessageBox.Show("已停止开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
发布打包程序的时候拖个程序的快捷方式到启动项里就完成了
写windows服务
最简单做法是把程序的快捷方式放到windous启动项目里面,这样windows会自己启动它
不错,不错,我去试试,呵呵……
C# code
/// <summary>
/// 设置程序开机启动
/// 或取消开机启动
/// </summary>
/// <param name="started">设置开机启动,或者取消开机启动</param>
/// <param name="exeName">注册表中程序的名字</param>
/// <param name="path">开机启动的程序路径</param>
/// <returns>开启或则停用是否成功</returns>
public static bool runWhenStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}
http://download.csdn.net/source/1972131
不错,收藏了!
感谢楼上的好心人 学习了
学习学习!
C# code public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWA……顶了…
不错,以后会用上,打个标记,以后来看。
我觉的你用程序生成一个快捷方式,然后复制到系统的启动项里,这样比较好一点。我以前做过一个自启动的程序,先写注册表启动,但是发现,每次通过注册表启动的,就不能通过webservice访问网络,后来没办法,直接加到启动项里就可以访问网络了。我想这可能与启动顺序有关系。
mark!
学习了!
支持一个!
学习中!
关注!+1
关注!~~~~
mark!
楼上好多了兄弟都给解答了 看来来晚了
用代码 写成服务怎么去写啊! 和多人希望高手去以服务那种形式, 写注册表 未免也带容易了吧!!
mark…….
Mark了,我也来学习学习
牛人多多
16楼正解,2楼不可行。
19楼正解,2楼不行。差点高错。误导其他人
16.,19都行,临机实验过