Set that a program has to run at startup from an installer

J4N picture J4N · Feb 23, 2011 · Viewed 28k times · Source

I've a C# .net 4 application, I'm starting to create the installer.

The installed program works fine, but my customer want that the application start with window(it's an "Always of top" toolbar which help them to manage their call).

I thought to create a "Class Installer", and in it insert a key in the registry.

I've two problems:

First: How can I find the executable path? It can change between installation. I found somewhere a Application.ExecutablePath, but it seems it located into Application.Window.Forms so I think its not compatible with WPF

Second: I need to insert this key in the registry of the local machine. Is there a way in a windows installer to specify that the user must have admin rights?

Here is the code I started:

RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("CstStart", ???);

Thank you very much for your help!

EDIT: I found that I can pass to my Custom action some data, with :

/DIR="[TARGETDIR]\"

in the CustomActionData.

Normally it's said I can retrieve it through the

this.Context.Parameters["DIR"];

But.... it seems that in my installer class, this.Context is null :(

EDIT2: The Context was null because I was doing this in the constructor, I'm now doing this in the event this.AfterInstall and now I get a context, which seems to contains a var "assemblypath" which contains exactly the path I need.

Answer

PeteH picture PeteH · Feb 23, 2011

My assumption is you're working from a Setup project here.....

one way to do this - I'm not saying it is the best but it is probably the easiest - is to do the following:

In the "File System" tab, right click on "File System on Target Machine", "Add Special Folder", "User's Startup Folder" and such folder appears on the list. Next, get a shortcut to your project output (by right clicking) and drag that shortcut into the Startup folder.

Last of all, since you want this for all users, go into the Deployment Project Properties and set "InstallAllUsers" to true.

Now, there is a caveat with this approach, in that the app does not start when Windows starts, but when someone logs on. From what you say of your project (a UI app) this may be ok.

If you really want to go hacking the registry you could look at creating a custom action dll. This is something where you can write pretty much what you like, and at the end of the install process the setup program will call into your dll so you can do your stuff. You can pass things like the path of the executable as a parameter into the dll (which you would pick up from the installer dialog, not from any object). I won't go into this is detail as there must be loads on the web about this.

You should be aware that there is a finite amount of information you can pass into the dll - might be as low as 256 chars if memory serves. You'll probably be ok with just a path but when you start doing more....

As regards detecting whether a user is a local Admin, that is a bit more tricky, if only because the custom action dll gets run at the end of the installation rather than at the start - so you could write code to find out what groups the current user is in, but the chances are you'd have blown up before that code could run. I don't believe - in a Microsoft Setup project at least - that it is possible to know this.