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

标签 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);
    }

最佳答案

您可以在启动文件夹中或通过注册表值向应用程序添加快捷方式(大多数应用程序使用注册表中的 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帮助教程。

关于c# - 如何在计算机启动时运行我的 winform 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276418/

相关文章:

c# - LightSwitch 和 VS 2010 Ultimate

c++ - 查看visual studio 2010中一个线程获得了多少锁?

.net - 您对 MS CAB(复合应用程序 block )有何看法?

.net - Microsoft.Build.Utilities.FileTracker 引发异常错误。发生在不同的项目中

c# - 如果新约束验证失败,则 DacServices 回滚

c# - 由于异常 : "Access denied for user ' root' @'localhost' (using password: YES)",无法从 C# 应用程序访问 MySQL

c# - 如何登录到由 Azure 移动 Web 服务自动生成的 Web 服务?

C# 示例语法问题

c# - 每天创建一个新的日志文件

c# - 在图片框内绘制矩形 SizeMode Zoom