マシンの起動後に自動的に実行されるアプリケーションを作成したいと考えています。
C#でどうすればいいのか、誰でも助けてくれますか?
これは、アプリをスタートアップに追加する方法です。
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (!IsStartupItem())
// Add the value in the registry so that the application runs at startup
rkApp.SetValue("My app's name", Application.ExecutablePath.ToString());
そしてそれを削除するには:
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if(IsStartupItem())
// Remove the value from the registry so that the application doesn't start
rkApp.DeleteValue("My app's name", false);
そして、私のコードの IsStartupItem 関数:
private bool IsStartupItem()
{
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rkApp.GetValue("My app's name") == null)
// The value doesn't exist, the application is not set to run at startup
return false;
else
// The value exists, the application is set to run at startup
return true;
}
アプリケーションの Windows サービスを作成し、その Windows サービスを登録し、スタートアップ プロパティを automatic に設定します。これで、Windows が起動するたびにサービスが自動的に開始され、次のリンクが表示されます: http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html
レジストリにキーを追加するよりも良い方法は、WindowsStartUp
フォルダーにショートカットを追加することだと思います。これは、ユーザーにとってより透過的であり、ユーザーがアプリケーションを必要としない場合は、ショートカットを削除することをユーザーに選択させることができます。 Windows の起動時に開始します。
プログラムがこれを実現する方法のほとんどはインストーラーを介して行われます。インストーラーは、起動時にプログラムが確実に開始されるようにレジストリを変更するなど、多くのことを実行できますが、この動作を無効にするオプションを常にユーザーに提供する必要があります。