user3492582
user3492582

Reputation: 81

Autostart when windows reboots

I'm trying to write a simple program in C sharp that should Autostart when Windows boots and keep working. I have tried several methods, which did not work though. Any suggestions? Note: I'm a beginner.

I tried:

var thisPath = System.IO.Directory.GetCurrentDirectory();
                    RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                    string name = Path.GetFileName(Application.ExecutablePath);
                    if (regKey == null) // Key not existing, we need to create the key.
                    {
                       Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");

                    }

                    regKey.SetValue(name, (string)path, RegistryValueKind.String);

and also:

string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
            WshShell shell = new WshShell();
            string shortcutAddress = startupFolder + @"\Mylibraryxd.lnk";
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup"; // set the description of the shortcut
            shortcut.WorkingDirectory = Application.StartupPath; /* working directory */
            shortcut.TargetPath = Application.ExecutablePath; /* path of the executable */
            shortcut.Save(); // save the shortcut 

None of these worked properly, do you have any ideas? Thank you!!!

Upvotes: 0

Views: 222

Answers (1)

Sandro Cutri
Sandro Cutri

Reputation: 68

You can press Windows + R and write shell:startup. if you have the file you can drag and drop it there or just create a link. Everything in that folder automaticly starts as soon as the user is logged in. That means you don't have to link it in the code itself.

Upvotes: 1

Related Questions