[英]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
看到这段代码的结果:
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.