如何在计算机启动时运行我的winform应用程序

时间:2014-08-13 00:49:11

标签: c# winforms visual-studio-2010 visual-studio system-tray

我只是想在计算机启动时运行我的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);
    }

1 个答案:

答案 0 :(得分:11)

您可以在启动文件夹中或通过注册表值为应用程序添加快捷方式(大多数应用程序使用注册表中的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中查看此代码的结果: http://i.imgur.com/vYC5PPr.png

Run key(以及用于运行应用程序的RunOnce键)将在启动时/用户登录时运行其中的所有应用程序。

这是我在我的应用程序中使用的代码,它运行良好。您不需要任何特殊的安装程序来执行此操作,您可以在每次应用程序启动时调用此方法,它将使用可执行文件的路径在注册表中的Run项中设置/更新应用程序值。

启动文件夹替代方案需要更多参与设置,请查看this教程以获取帮助。

相关问题