惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 8user

partial关键字的含义和使用 JS仿Open()打开一个新窗口,并不是弹出新窗口 判断是否含有汉字 - 8user - 博客园 如何统计代码行执行的时间? - 8user - 博客园 时间的正则表达式验证 深入探讨IsPostBack【转】 IIS无法启动的解决方法 FCKEditor的使用及配置方法 Cool MVC: 使用正则式限定路由规则Rount - 8user C#中的值类型和引用类型 C#在线备份数据库至制定目录 - 8user - 博客园 c#实现天气预报查询 C#中的枚举器 C#里面比较时间大小三种方法 - 8user - 博客园 c#中删除ArrayList中的重复数据 - 8user - 博客园 SQL Server数据库开发的二十一条规则 VS2008 快捷键大全 C#调用dos命令 反射性能测试
c#实现程序的开机启动 - 8user - 博客园
8user · 2009-06-10 · via 博客园 - 8user

c#实现程序的开机启动大致有两种方法,就是写入注册表或者采用服务程序,最近一直研究着用C#来操作注册表,下面介绍的方法便是用注册表来实现程序随开机启动。

关键代码很短,如下:

前提要引入命名空间 using Microsoft.Win32;

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项

key.SetValue(程序的名称, 程序的路径);//设置为开机启动

为了安全性,还应该判断设置的文件是否存在以及该子项是否存在,所有完整代码如下:


using Microsoft.Win32;

private void btnShowOpen_Click(object
 sender, EventArgs e)
        
{
            OpenFileDialog open 
= new
 OpenFileDialog();
            open.Filter 
= "可执行文件(*.exe)|*.exe"
;
            
if (open.ShowDialog() ==
 DialogResult.OK)
            
{
                txtPath.Text 
=
 open.FileName;
            }

        }


        
private 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;
        }


        
private void btnSet_Click(object sender, EventArgs e)
        
{
            
if (txtPath.Text == "")//检查是否选择了文件

            {
                MessageBox.Show(
"请选择要随计算机一起启动的程序路径!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Focus();
                
return
;
            }
            
            
string path =
 txtPath.Text.Trim();
            
string exeName = path.Substring(path.LastIndexOf("\\"+ 1
);
            
if (!File.Exists(path))//检查该文件是否存在

            {
                MessageBox.Show(
"文件不存在!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Text 
= ""
;
                txtPath.Focus();
                
return
;
            }

            
if (runWhenStart(true,exeName, path))
            
{
                MessageBox.Show(
"设置成功!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
else
            
{
                MessageBox.Show(
"设置失败!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }


        
private void btnCancel_Click(object sender, EventArgs e)
        
{
            
if (txtPath.Text == "")//检查是否选择了文件

            {
                MessageBox.Show(
"请选择要撤销随计算机一起启动的程序路径!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Focus();
                
return
;
            }

            
string path = txtPath.Text.Trim();
            
string exeName = path.Substring(path.LastIndexOf("\\"+ 1
);
            
if (!File.Exists(path))//检查该文件是否存在

            {
                MessageBox.Show(
"文件不存在!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Text 
= ""
;
                txtPath.Focus();
                
return
;
            }

            
if (runWhenStart(false, exeName, path))
            
{
                MessageBox.Show(
"撤销成功!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
else
            
{
                MessageBox.Show(
"撤销失败!""消息"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }