0

I have checked some other examples.

I am working on a WinForm. & now i am going to create its installer.

i want to start the software when windows starts & trying to use the following code.

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());

But i am confused that where to put this code. i don't want this code executed every time when my application starts as it will executed once when the software is installed.

shall i put check on form_Load() if the regkey is absent, & if yes then add this value to the Run.

is it correct ? i don't want to give an option to the user & i want to start this application at the startup compulsorily.

Thanks.

Vizel Leonid
  • 456
  • 7
  • 15
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • 1
    How about you just drag a shortcut of your EXE to the 'Startup' section in Start->All Programs->Startup ? EDIT: My bad, that's not the right way. you *COULD* copy a shortcut of your EXE to the startup folder upon installation. I guess using the registry is a better way. – Shai Feb 07 '12 at 08:08
  • 1
    This code belongs into the installer of your program. – Daniel Hilgarth Feb 07 '12 at 08:09
  • 1
    `i don't want to give an option to the user & i want to start this application at the startup compulsorily.` You might have your reasons, but not giving the user the option is usually considered rude. – Heinzi Feb 07 '12 at 08:25
  • Don't worry user wont consider it rude.. – Sangram Nandkhile Feb 07 '12 at 08:32

5 Answers5

1

As the others users have commented, this kind of thing should go on the installer. On the setup project you can create with Visual Studio, you can add a link to the user's startup folder that should do the trick.

BUT

If you don't want the user tampering with the startup folder and you absolutely want the program to start automagically with Windows, you could do as you've said: check for the adequate registry key everytime the program starts, and if it's not there write it. Take into account your program will need elevated account rights for this.

BUT

Do you ABSOLUTELY need this? Are you absolutely sure you can't offer it as an option to the final user? If I was a user to your program, didn't want it on startup, take the effort to remove a registry key to get it out of there and then find out it's again in the registry without my consent, I'd be pretty pissed...

CMPerez
  • 905
  • 1
  • 10
  • 25
1

You can create your customAction class for your installer like this: http://msdn.microsoft.com/en-us/library/d9k65z2d.aspx#Y0

Once you have this class, put your registry code in the commit section. Now, whenever your application will be installed, your application will be added to registry to run at startup.

Harsh
  • 3,683
  • 2
  • 25
  • 41
0

Make a small console application that uses the line and run that at the end of your installation.

linkerro
  • 5,318
  • 3
  • 25
  • 29
0

It depends on the istaller software that you use. If you use the setup project template provided by Visual Studio, you can have the registry key created automatically.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I am creating msi by adding `setup & deployment`..can you suggest any software which is easy to use? – Sangram Nandkhile Feb 07 '12 at 08:28
  • @CodeHippo: Yes, that's it. Open the Registry Editor in your setup and deployment project (right click on the setup project in Solution Explorer, then select View -> Registry). There, you can add your registry keys. – Heinzi Feb 07 '12 at 08:51
  • @Code Hippo:this is the really good tool http://www.advancedinstaller.com/ for installation project u can purchase it or can use the freeware edition – Smack Feb 07 '12 at 09:38
0

Your installer should create the registry keys.

In addition, your installer may also ask whether to install for the current user or for all users. If user says everyone, then the key must be added in HKEY_LOCAL_MACHINE rather than HKEY_CURRENT_USER, which requires admin privileges. Installers do usually have that privilege .

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • I am creating msi by adding `setup & deployment`..can you suggest any software which is easy to use? – Sangram Nandkhile Feb 07 '12 at 08:29
  • I've always despised the tools included in Visual Studio for creating installation packages. Back in the day I used Inno Setup (http://www.jrsoftware.org/isinfo.php) which is free, quite easy to use and pretty powerful. – CMPerez Feb 07 '12 at 08:37
  • A quick google run has also revealed NSIS ( http://nsis.sourceforge.net/Main_Page ) and WiX ( http://wix.sourceforge.net/ ), but I've never used any of these, so I can't comment on them. I know NSIS is quite widely used, though. – CMPerez Feb 07 '12 at 08:40
  • There is a ton of questions related to choosing a installer tool. Such as [this one](http://stackoverflow.com/questions/458900) and [this one](http://stackoverflow.com/questions/3767) – Serge Wautier Feb 07 '12 at 10:34