我只是想在计算机启动时运行我的 winform 应用程序。我已将任务栏图标显示在系统托盘的左侧。我的功能一切正常。但是我需要这样一种方式,如果我在计算机上安装 winform。我需要它在计算机手动或自动重启后运行。
现在就像如果我重新启动应用程序,我需要再次启动应用程序才能运行它。 但是我需要在系统重启时自动启动应用程序之类的东西。任何想法。
我正在尝试的代码
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
最佳答案
您可以在启动文件夹中或通过注册表值向应用程序添加快捷方式(大多数应用程序使用注册表中的 HKLM or HKCU\Software\Microsoft\Windows\CurrentVersion\Run
键或将快捷方式放在 C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
文件夹中。还有其他选项,但这些是最受欢迎的)
示例:
Microsoft.Win32;
...
//Startup registry key and value
private static readonly string StartupKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
private static readonly string StartupValue = "MyApplicationName";
...
private static void SetStartup()
{
//Set the application to run at startup
RegistryKey key = Registry.CurrentUser.OpenSubKey (StartupKey, true);
key.SetValue(StartupValue, Application.ExecutablePath.ToString());
}
您可以在 regedit
中查看此代码的结果:
Run
key (也可以使用 RunOnce
键来运行您的应用程序一次)将在启动时/用户登录时运行其中的所有应用程序。
这是我在应用程序中使用的代码,效果很好。您不需要任何特殊的安装程序来执行此操作,您只需在每次应用程序启动时调用此方法,它就会设置/更新 Run
中的应用程序值。在注册表中键入可执行文件的路径。
启动文件夹的设置有点复杂,查看this帮助教程。
关于c# - 如何在计算机启动时运行我的 winform 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276418/