WPFブートコード

public static class AutoStartManager{
    
    
 private const string RunRegistryKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

 public static bool IsAutoStartEnabled()
 {
    
    
 using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RunRegistryKey, false))
 {
    
    
 return key.GetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName) != null;
 }
 }

 public static void EnableAutoStart()
 {
    
    
 using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RunRegistryKey, true))
 {
    
    
 key.SetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName, System.Reflection.Assembly.GetEntryAssembly().Location);
 }
 }

 public static void DisableAutoStart()
 {
    
    
 using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RunRegistryKey, true))
 {
    
    
 key.DeleteValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName, false);
 }
 }
}

//次に、WPF アプリケーションで、起動時の自動起動を設定する必要がある EnableAutoStart() メソッドを呼び出し、アプリケーションを自動起動項目に追加します。たとえば、アプリケーションの設定ページまたは起動ウィンドウにチェックボックスを提供して、自動起動機能を有効にするかどうかをユーザーが選択できるようにすることができます。

{
    
    
 AutoStartManager.EnableAutoStart();
}
else{
    
    
 AutoStartManager.DisableAutoStart();
}

//レジストリの変更には管理者権限が必要なので、コードを実行するときは、アプリケーションが管理者として実行されていることを確認してください。さらに、自動起動機能は通常、コンピュータの起動直後ではなく、ユーザーのログイン時に開始されます。

おすすめ

転載: blog.csdn.net/BeanGo/article/details/133386367