1

If you type "regedit" in the Start menu's edit box and mash the Enter key, Registry Editor will be invoked. The same is true for "cmd" and the Command Line, and doubtless several other apps.

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?

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).

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.

UPDATE

I added the code recommended by Chandan (with my executable's name):

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:

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.

UPDATE 2

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):

enter image description here

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?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    `HKLM\Softoware\Microsoft\Windows\CurrentVersion\App Paths` The search menu can find it without that entry if it is installed to Program Files, or in some MRU lists IIRC – Ňɏssa Pøngjǣrdenlarp May 13 '16 at 23:29
  • Does it have to be programmatically added to "Program Files" (which doubtless "messes with" the Registry), or would simply copying it into a folder there do the trick? – B. Clay Shannon-B. Crow Raven May 13 '16 at 23:39
  • All the apps I wrote for myself were never installed, and some do not have a start menu entry, but it can find them without that AppPaths entry. I am not certain of all the magic (hence no proper answer) but I think it finds files installed to `Program Files` folder or which have a Start Menu entry or that you have searched/run before. My search finds lots of things which do not have an entry like Foo.EXE and UninstallFoo.exe. It probably also searches for Click once apps – Ňɏssa Pøngjǣrdenlarp May 13 '16 at 23:48
  • I don't think location matters, just that it is registered in the registry. You can read up on it on [**MSDN**](https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx). – Visual Vincent May 13 '16 at 23:55
  • You can use `regedit` to examine the existing entries in HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths` to see what should be included and which key it should be in. There is no *folder there* to copy into; it's a registry key which an administrative user needs to edit. The proper way to do so is via your installer, which requires an administrator to run; HKLM requires admin rights to modify, which precludes doing it on the first run of your application. – Ken White May 14 '16 at 00:07

2 Answers2

4

You can add your application's parent directory's path to the environment variable called 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.

You can also change EnvironmentVariableTarget.Machine to EnvironmentVariableTarget.User to modify the variable for the current user only.


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.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • 1
    There's a problem with adding to the PATH, which is that there's a limit to the number of characters total in that entry. There's not much point in altering the PATH for a single executable; an entry in the registry entry mentioned by @Plutonix in a comment is better. It should be done by your software's installer (which is run by an administrative user), as a standard user can't write to HKLM. – Ken White May 14 '16 at 00:02
0

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 + "\"");
    }
}
Chandan Gupta
  • 452
  • 4
  • 15