V
V
ViceInc2021-02-11 17:26:26
Automation
ViceInc, 2021-02-11 17:26:26

C# How to programmatically add an application to Windows startup?

Hello! I'm trying to programmatically add my application to startup. However, when the system starts, the application opens and immediately closes. When running the application manually, it works. I add it to autoload in the following way

private void installButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                richTextBox_OK.Text = "";
                richTextBox_ERROR.Text = "";

                string dirPath = folderBrowserDialog.SelectedPath + "\\AVUC";
                string fileExePath = folderBrowserDialog.SelectedPath + "\\AVUC\\AVUC Microsoft Version Controller.exe";
                string fileLinkPath = "C:\\Users\\" + userName + "\\AppData\\Roaming\\Microsoft\\Windows" +
                    "\\Start Menu\\Programs\\Startup\\AVUC Microsoft Version Controller.lnk";
                string fileTxtPath = folderBrowserDialog.SelectedPath + "\\AVUC\\configLocal.txt";

                try
                {
                    richTextBox_OK.Text += "UserName: " + userName + "\n";

                    if (!Directory.Exists(dirPath)) 
                    { 
                        Directory.CreateDirectory(dirPath);
                        richTextBox_OK.Text += "Directory \"" + dirPath + "\" created\n";
                    }
                    else
                    {
                        richTextBox_ERROR.Text += "ERROR!\tDirectory \"" + dirPath + "\" is already exists\n";
                        richTextBox_ERROR.Text += "NOT INSTALLED\n";
                        return;
                    }

                    File.Copy("AVUC Microsoft Version Controller.exe", fileExePath);
                    richTextBox_OK.Text += "File \"AVUC Microsoft Version Controller.exe\" copy created\n";
                    File.Copy("configLocal.txt", fileTxtPath);
                    richTextBox_OK.Text += "File \"configLocal.txt\" copy created\n";

                    ShortCut.Create(fileExePath, fileLinkPath, "", "AVUC Microsoft Version Controller");
                    richTextBox_OK.Text += "Link created: " + fileLinkPath + "\n";

                    richTextBox_OK.Text += "\tINSTALLED\n";
                }
                catch (Exception ex)
                {
                    richTextBox_ERROR.Text += "ERROR!\t" + ex.Message + "\n";
                    richTextBox_ERROR.Text += "\tNOT INSTALLED\n";
                }
            }
        }


Create a shortcut
ShortCut.Create(fileExePath, fileLinkPath, "", "AVUC Microsoft Version Controller");

borrowed here https://www.cyberforum.ru/csharp-net/thread177408.html

If you manually create a shortcut and place it in the startup folder, it works smoothly. For some reason he doesn't like programmatically created shortcuts or the shortcut creation method is obtained. If someone is busy - tell me pliz))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2021-02-11
@ViceInc

Save and import into the registry, you can do the same in the installer or when setting up the program.
hcr.reg

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"OneDrive"="\"C:\\Users\\kvn\\AppData\\Local\\Microsoft\\OneDrive\\OneDrive.exe\" /background"
"CCXProcess"="\"C:\\Program Files (x86)\\Adobe\\Adobe Creative Cloud Experience\\CCXProcess.exe\""
"com.squirrel.Teams.Teams"="C:\\Users\\kvn\\AppData\\Local\\Microsoft\\Teams\\Update.exe --processStart \"Teams.exe\" --process-start-args \"--system-initiated\""
"Discord"="C:\\Users\\kvn\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe"

M
Maxim Siomin, 2021-02-11
@MaxSiominDev

Try to run a python script from a c# program, everything should work with it

import os
import winshell
import inspect
from getpass import getuser
from win32com.client import Dispatch


filename = inspect.getframeinfo(inspect.currentframe()).filename
user = getuser()

wDir = os.path.dirname(os.path.abspath(filename))
target = wDir + r'\program_name.exe'
path = rf'C:\Users\{user}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\program_name.lnk'
icon = wDir + r'\icon_name.ico'
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question