jeudi 10 avril 2014

c# - Autostart Lorsque windows redémarre - Stack Overflow


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!!!



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!!!


0 commentaires:

Enregistrer un commentaire