10
votes

Placer un raccourci dans le dossier de démarrage de l'utilisateur pour commencer avec Windows

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).

sur le redémarrage de Windows, il chargera mon application automatiquement.

Comment cela peut-il être fait?


0 commentaires

3 Réponses :


14
votes

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);


0 commentaires

0
votes
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);



        }

0 commentaires

5
votes
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();

1 commentaires

Pour ajouter une référence à "iwshruntimelibary" Sélectionnez "Modèle d'objet hôte de script Windows" sous com