[英]How can I get my app to run from the Start menu?
If you type "regedit" in the Start menu's edit box and mash the Enter key, Registry Editor will be invoked. 如果在“开始”菜单的编辑框中键入“ regedit”并混入Enter键,则将调用注册表编辑器。 The same is true for "cmd" and the Command Line, and doubtless several other apps. 对于“ cmd”和命令行,无疑还有其他几个应用程序也是如此。
How can I get my app to respond the same way, so that if the user enters "Platypus" in the Start menu edit box, Platypus.exe will be invoked? 如何使我的应用程序以相同的方式响应,以便如果用户在“开始”菜单编辑框中输入“ Platypus”,则将调用Platypus.exe?
Does it require manipulation of the Registry / adding an entry somewhere there, and if so, just what key and value needs to be added? 是否需要操纵注册表/在该处添加条目,如果需要,仅需要添加什么键和值?
I would be satisfied with the user needing to run the app manually once (2-clicking its icon; it's a Winforms app), at which time startup code (no pun intended) would do whatever was necessary to make the app henceforth Startsmartable (Windows key, "Platypus", to start the app). 我对用户需要手动运行一次该应用程序(两次单击它的图标;这是一个Winforms应用程序)感到满意,此时启动代码(无双关语)将做使该应用程序从此开始可运行的一切必要操作(Windows键“ Platypus”以启动该应用)。
I know that it's just as easy/easier for the user to simply 2-click a desktop icon when they want to run the app, but this particular functionality is not my idea, so complaints about the oddity of this question would be to no avail. 我知道,当用户想要运行应用程序时,只需简单地单击2桌面图标即可,这同样容易/容易,但是此功能不是我的主意,因此抱怨这个问题的可能性是无济于事的。
I added the code recommended by Chandan (with my executable's name): 我添加了Chandan推荐的代码(带有可执行文件的名称):
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("RoboReporter", "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
}
}
...called it from the main form's load event: ...是从主表单的load事件中调用的:
private void FormRoboReporter_Load(object sender, EventArgs e)
{
RoboReporterConstsAndUtils.AddToStartup();
}
...shut down the app, went to the Start menu and entered the program's name ("RoboReporter"), and all it did was bring up search results of related file names. ...关闭该应用程序,转到“开始”菜单,然后输入程序的名称(“ RoboReporter”),然后所做的只是显示相关文件名的搜索结果。
What it does do is cause my app to run whenever the computer is restarted. 它所做的是导致我的应用程序在计算机重新启动时运行。 That's not what I want. 那不是我想要的 The code above adds an entry to HKEY_CURRENT_USER.Software.Microsoft.Windows.CurrentVersion.Run as can be seen here (along with a couple of other entries that predated it): 上面的代码在HKEY_CURRENT_USER.Software.Microsoft.Windows.CurrentVersion.Run中添加了一个条目,如此处所示(以及之前的其他两个条目):
I don't want the app to start up every time the computer restarts, so I removed the entry. 我不希望每次计算机重新启动时都启动该应用程序,因此我删除了该条目。 The question remains: how can I make the app runnable from the Start menu? 问题仍然存在:如何从“开始”菜单使应用程序可运行?
You can add your application's parent directory's path to the environment variable called PATH
. 您可以将应用程序的父目录的路径添加到名为PATH
的环境变量中。
string pathvar = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", pathvar + ";" + Application.StartupPath + "\\", EnvironmentVariableTarget.Machine);
(Note that the paths added to this variable should end with a backslash \\
, and each path is separated by a semicolon ;
) (请注意,添加到此变量的路径应以反斜杠\\
结束,并且每个路径均以分号分隔;
)
Adding the parent directory's path to the environment variable will make all it's contents quickly accessible from the Start Menu's search field, from Run
and from CMD
. 将父目录的路径添加到环境变量将使它的所有内容都可以从开始菜单的搜索字段, Run
和CMD
快速访问。
You can also change EnvironmentVariableTarget.Machine
to EnvironmentVariableTarget.User
to modify the variable for the current user only. 您也可以将EnvironmentVariableTarget.Machine
更改为EnvironmentVariableTarget.User
以仅针对当前用户修改变量。
EDIT: 编辑:
A note: Setting a variable for the entire machine (by using EnvironmentVariableTarget.Machine
) seems to require elevated privileges when done from one's application. 注意:从一个人的应用程序完成时,通过使用EnvironmentVariableTarget.Machine
为整个机器设置一个变量似乎需要提升的特权。
you might want to run this 您可能要运行此
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("Name_of_your_Program", "\"" + Application.ExecutablePath + "\"");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.