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

5 c# system-tray visual-studio-2010 visual-studio winforms

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

Cyr*_*ral 13

您可以在启动文件夹中或通过注册表值为应用程序添加快捷方式(大多数应用程序使用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());
}
Run Code Online (Sandbox Code Playgroud)

您可以在以下位置查看此代码的结果regedit: http://i.imgur.com/vYC5PPr.png

Run(还或者在RunOnce一次运行应用程序键),将运行在它启动的所有应用程序/当用户登录.

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

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

  • 如果您在用户启动文件夹中创建快捷方式,它将起作用。要解决您看到的错误,请通过右键单击项目 &gt; 添加引用并找到它来包含对 Win32 的引用。 (2认同)