Placing a shortcut in the user's startup folder for starting with Windows

I wanted to give my user the "Start with Windows" feature. When the user checks this option, he places the shortcut icon in the startup folder (not in the registry).

When you restart Windows, it will automatically download my application.

How can I do that?

+4
source share
3 answers

you can use the Enviroment.SpecialFolder enumeration, although depending on your requirements, you can look at creating a Windows service, and not at the application that should start at startup.

File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname); 

edit:

File.Copy simply takes the file and file into the path for copying the file. The key in this fragment is Enviroment.GetFolderPath (Enviroment.SpecialFolder.Startup), which gets the path to the startup folder for copying.

You can use the above code in several ways. If you had an installer project for your application, you can run something like this during installation. Another way could be when the application launches it, checks if the shorcut exists and whether it places it there if not (File.Exists ()).

Here is the question of creating shortcuts in code.

+12
source
 WshShell wshShell = new WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut; string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // Create the shortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut( startUpFolderPath + "\\" + Application.ProductName + ".lnk"); shortcut.TargetPath = Application.ExecutablePath; shortcut.WorkingDirectory = Application.StartupPath; shortcut.Description = "Launch My Application"; // shortcut.IconLocation = Application.StartupPath + @"\App.ico"; shortcut.Save(); 
0
source
 private void button2_Click(object sender, EventArgs e) { string pas = Application.StartupPath; string sourcePath = pas; string destinationPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"; string sourceFileName = "filename.txt";//eny tipe of file string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName); string destinationFile = System.IO.Path.Combine(destinationPath); if (!System.IO.Directory.Exists(destinationPath)) { System.IO.Directory.CreateDirectory(destinationPath); } System.IO.File.Copy(sourceFile, destinationFile, true); } 
-one
source

All Articles