简体   繁体   English

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

[英]How to run my winform application when computer starts

I am just trying to run my winform application when computer starts. 我只是想在计算机启动时运行我的winform应用程序。 I have made the taskbar icon to appear on the left hand side of the system tray. 我已将任务栏图标显示在系统托盘的左侧。 My function everything is working well. 我的功能一切运作良好。 But I need in such a way that if I install the winform in a computer. 但我需要这样一种方式,如果我在计算机中安装winform。 I need this to run after the computer restarts manually or automatically. 我需要在计算机手动或自动重启后运行。

For now its like if I restart the application I again need to launch the application to run it . 就目前而言, 如果我重新启动应用程序,我再次需要启动应用程序来运行它 But I need something like to atuomatically launch the application on system restart. 但我需要在系统重启时自动启动应用程序。 Any idea. 任何想法。

Codes i am trying 我正在尝试的代码

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

You can add a shortcut to your application in the startup folder, or through a registry value (Most applications use the HKLM or HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run key in the registry or put a shortcut in the C:\\Users\\<user name>\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup folder. There are other options but these are the most popular) 您可以在启动文件夹中或通过注册表值为应用程序添加快捷方式(大多数应用程序在注册表中使用HKLM or HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run键或在C:\\Users\\<user name>\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup快捷方式C:\\Users\\<user name>\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup文件夹。还有其他选项,但这些是最受欢迎的)

Sample: 样品:

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());
}

You can see the results of this code in regedit : 你可以在regedit看到这段代码的结果: http://i.imgur.com/vYC5PPr.png

The Run key (And alternatively the RunOnce key for running your application once) will run all applications that are in it on startup/when a user logs on. Run (以及用于运行应用程序的RunOnce键)将在启动时/用户登录时运行其中的所有应用程序。

This is the code I use in my applications, and it works great. 这是我在我的应用程序中使用的代码,它工作得很好。 You don't need any special installer to do this, you can simply call this method every time your app starts and it will set/update the applications value in the Run key in the registry with the path to the executable. 您不需要任何特殊安装程序来执行此操作,您可以在每次应用程序启动时调用此方法,它将使用可执行文件的路径在注册表中的Run键中设置/更新应用程序值。

The startup folder alternative is a little more involved to set up, check out this tutorial for help. 启动文件夹替代方案需要更多参与设置,请查看教程以获取帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们启动电脑时如何启动windows应用程序 - How to start the windows application when we starts the computer 在开发计算机上成功运行。 但是当我在其他计算机上运行打包的winform应用程序时,无法加载z3.dll - run successfully on development computer. But fail to load z3.dll when I run the packaged winform application on other computer 当winform应用程序关闭时,如何停止线程 - How do I stop a thread when my winform application closes 系统启动时如何自动运行应用程序? - How do I automatically run an application when the system starts? 如何在C#应用程序启动时正确运行代码? - How to run code right when a C# application starts? 如何从控制台应用程序运行 winform? - how to run a winform from console application? 如何运行该数据库备份并从Winform应用程序还原脚本? - How do I run that database backup and restore scripts from my winform application? 如何减少Winform应用程序的内存消耗 - How to reduce memory consumption of my winform application 我的winform按钮在按下时会产生复选框,当选中一个时,如何让代码运行? - My winform button spawns checkboxes when pressed, how can I let code run when one is checked? 如何在系统空闲时通过窗口服务运行winform应用程序 - how to Run an winform application through window service when system goes idle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM