【问题标题】:How to run my winform application when computer starts计算机启动时如何运行我的winform应用程序
【发布时间】:2014-08-13 00:49:11
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我什么也没得到。我理解我在 codeproject 中找到的文章只是在同一目录中创建了一个快捷方式。我需要一些东西来自动..在 Windows STARTUP 上运行我的应用程序...不创建快捷方式..
    • 另外我收到了一个名为"The type or namespace name 'Win32' does not exist in the namespace 'System' "的错误
    • 如果您在用户启动文件夹中创建快捷方式,它将起作用。要解决您看到的错误,请通过右键单击项目 > 添加引用并找到它来包含对 Win32 的引用。
    • 没问题,很高兴我能帮上忙!
    • 所以当我给命名空间using Microsoft.Win32;它工作...非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2013-08-14
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多