如何在计算机启动时运行我的WinForm应用程序 [英] How to run my winform application when computer starts

查看:313
本文介绍了如何在计算机启动时运行我的WinForm应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在计算机启动时运行我的WinForm应用程序。我所做的任务栏图标出现在系统托盘的左侧。我的功能一切正常良好。但我需要,如果我安装的winform在计算机这样一种方式。我需要这电脑手动或自动重启后运行。



现在它像如果我重新启动应用程序,我再次需要启动应用程序来运行它
,而我需要的东西喜欢atuomatically发动系统重新启动应用程序。任何想法。



代码我试图

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
timer1.Start();
notifyIcon1.BalloonTipTitle =最小化到托盘应用程序;
notifyIcon1.BalloonTipText =您已经成功地最小化您的形式。
notifyIcon1.Visible = TRUE;
notifyIcon1.ShowBalloonTip(100);
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = FALSE;
}

私人无效notifyIcon1_MouseDoubleClick(对象发件人,MouseEventArgs E)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = TRUE;
}

私人无效exitToolStripMenuItem_Click(对象发件人,EventArgs五)
{
System.Environment.Exit(0);
}


解决方案

您可以添加快捷方式您在启动文件夹的应用程序,或通过注册表值(大多数应用程序使用在 HKLM或HKCU\Software\Microsoft\Windows\CurrentVersion\Run 键注册表或放一个快捷方式在 C:\Users\<使用者名称> \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 文件夹中还有其他的选择,但这些是最流行的)



示例:

 的Microsoft.Win32; 
...

//启动注册表项和值
私人静态只读字符串StartupKey =SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ \\\跑;
私人静态只读字符串StartupValue =MyApplicationName;

...
私有静态无效SetStartup()
{
//设置为在启动
的RegistryKey键= Registry.CurrentUser运行应用程序。 OpenSubKey(StartupKey,真);
key.SetValue(StartupValue,Application.ExecutablePath.ToString());
}

您可以看到这段代码的结果注册表编辑器



运行(备选地在的RunOnce 键一次运行你的应用程序)将运行/是在它启动时,当用户登录的所有应用程序。



这是我在应用程序中使用的代码,它的伟大工程。你不需要任何特殊的安装要做到这一点,你可以简单地调用此方法每次你的应用程序启动时,它会设置/更新在运行键应用价值。注册表的可执行文件路径



启动文件夹的选择是一个涉及多一点成立,检查出的此内容教程的帮助。


I am just trying to run my winform application when computer starts. 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. 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)

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:

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.

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.

The startup folder alternative is a little more involved to set up, check out this tutorial for help.

这篇关于如何在计算机启动时运行我的WinForm应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆