Je voulais donner à mon utilisateur une option pour "Démarrer avec Windows". Lorsque l'utilisateur vérifie cette option, il placera une icône de raccourci dans le dossier de démarrage (pas dans le registre). p>
sur le redémarrage de Windows, il chargera mon application automatiquement. P>
Comment cela peut-il être fait? P>
3 Réponses :
Vous pouvez utiliser l'envioment.Specialfolder Enum, bien que, selon vos exigences, vous pouvez envisager de créer un service Windows au lieu d'une application qui doit démarrer au démarrage.
File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname);
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);
}
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();
Pour ajouter une référence à "iwshruntimelibary" Sélectionnez "Modèle d'objet hôte de script Windows" sous com