关闭

C#實現自動啟動

624人阅读 评论(0) 收藏 举报
private void toolStripMenuItem3_Click(object sender, EventArgs e)
        
{
            
if (autorun == true)
            
{
                RunWhenStart(
false, Application.ProductName, Application.StartupPath + @"" + Application.ProductName+@".exe");
                autorun 
= false;
            }

            
else
            
{
                RunWhenStart(
true, Application.ProductName, Application.StartupPath + @"" + Application.ProductName + @".exe");
                autorun 
= true;
            }

            Config a 
= new Config();
            a.WriteConfig(
"autorun", autorun.ToString());
            toolStripMenuItem3.Checked 
= autorun;
        }


        
public static void RunWhenStart(bool Started, string name, string path)
        
{
            RegistryKey HKLM 
= Registry.LocalMachine;
            RegistryKey Run 
= HKLM.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
            
if (Started == true)
            
{
                
try
                
{
                    Run.SetValue(name, path);
                    HKLM.Close();
                }

                
catch (Exception Err)
                
{
                    MessageBox.Show(Err.Message.ToString(), 
"友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

            
else
            
{
                
try
                
{
                    Run.DeleteValue(name);
                    HKLM.Close();
                }

                
catch (Exception)
                
{
                    MessageBox.Show(Err.Message.ToString(), 
"友情提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

        }
 
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:223630次
    • 积分:3683
    • 等级:
    • 排名:第9120名
    • 原创:131篇
    • 转载:33篇
    • 译文:0篇
    • 评论:43条
    最新评论