2

To make my application to start on Windows sturup I decided to put a shortcut to Startup folder.

I tried to use:

File.Move(AppDomain.CurrentDomain.BaseDirectory + "ApplicationName.exe", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");

It works, but it moves my shortcut not to the folder I need.

Environment.GetFolderPath(Environment.SpecialFolder.Startup)

works well, it returns:

C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

But my shortcut appears in

C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

Just 1 folder "behind".

File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");

also works "strange". It actually deletes this file, but again not in "Startup" folder.

If I try to manually add "\Startup" to the path like this:

Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"Startup\ApplicationName.lnk"

I get a System.IO.Excseption.

I can't type this path manually, I neen my application to work at diferent PCs with different versions of Windows. I also can't use Registry to make my application start with windows startup.

I use Windows 7, Visual Studio 2010, .NET 4.0, this is a WPF project.

Any ideas?

Pavlo Hermanov
  • 472
  • 5
  • 8

2 Answers2

3

Did you tried Environment.SpecialFolder.CommonStartup instead of Startup, I don't know why startup is not working for your requirement. Most of installer package do this for you; why do you want to do this for your self? Any reason not for using Registry?

I tried this code on my machine

var startup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
string file = Path.Combine(startup, "MyApp.lnk");
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Test");
}

And its coming on my startup

enter image description here

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • CommonStartup has the same problem. It copies file to C:\ProgramData\Microsoft\Windows\Start Menu\Programs. – Pavlo Hermanov Jul 22 '11 at 11:59
  • CommonStartup has the same problem. It copies file to C:\ProgramData\Microsoft\Windows\Start Menu\Programs. I want to do this manually to give users opportunity to switch off and on this function. I dont want to use Registry because accses to it may be blocked on different users. – Pavlo Hermanov Jul 22 '11 at 12:06
  • Look my Startup folder in Start menu – Anuraj Jul 22 '11 at 12:07
  • Thanks a lot, your code works well. I used File.Copy() instead of using StreamWriter, and file appers in correct folder. I think it is about Path.Combine(), but I don't understand how it works. Thanks again. – Pavlo Hermanov Jul 22 '11 at 12:13
  • Welcome. Make it an answer :) – Anuraj Jul 22 '11 at 12:14
  • "Any reason not for using Registry?" WTF? Why on earth would you trawl through the Registry when there's a *documented, supported* API for doing exactly what you want? (A with less code?) – Joe White Jul 22 '11 at 12:14
2

You should use System.IO.Path.Combine() so that you won't create StartupApplication1.exe. Note the missing backslash.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536