微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#编写的windows程序随系统启动的问题

 

C#写了一个windows的程序,想让它随系统启动运行

--------------

把可执行文件的快捷方式复制到启动文件夹里面,这样不安全,安全的方法是把系统做成WinService的方式,以系统服务的方式安全好多

--------------

设置某程序随系统启动自动运行,取消自动运行。 使用到using Microsoft.Win32;名称空间。

public 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);
                    MessageBox.Show("设定成功!","提示");
                }  
                catch 
                {  
                    //throw new Exception(ex.ToString());  
                }  
                finally 
                {  
                    if (reg != null)  
                        reg.Close();  
                }  
         }

使用此方法可设置某程序自动运行和取消自动运行。fileName:设置(取消)自动运行程序的完整地址,isAutoRun:是否设置自动运行和取消自动运行。true,自动运行。false,取消自动运行。

原理:操作注册表。

--------------

http://www.ziyouxue.net/2009/0804/5970.html

http://topic.csdn.net/t/20050225/16/3806744.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐