SoFunction
Updated on 2025-04-20

C# realizes the software startup function (no administrator permission required)

Introduction to the principle

References to this articleTwo common methods for C# to realize automatic software startup, encapsulate the first method inside intoAutoStartClasses can be done in two or three lines of code when used.

The principle of self-start isCreate a shortcut to the computer's automatic startup directory (no administrator permission required), this method is more general and has fewer restrictions.

How to use

How to use it is as follows:

//The default value of the shortcut description and name is the current process name. The default value of the automatic startup is the normal window. Generally, there is no need to set it manually.//Set the description of the shortcut, = "Software Description";
//Set the name of the shortcut = "Software Name";
//Set the self-start window type, the background service software can be set to the minimum window = ;

//When the shortcut is set true, if there is, if there is, if there is, if there is, if there is no, create it. Only one self-start shortcut can exist.//Set the power-on start, true starts, false not starts();
//Set desktop shortcuts, true to create desktop shortcuts (skip if there is, create if there is no), false to delete desktop shortcuts(true);

Complete code

Reference to the following namespace:

//Add a reference and search for Windows Script Host Object Model in Comusing IWshRuntimeLibrary;
using System;
using ;
using ;
using ;

AutoStartClass code:

public class AutoStart
{
    #region Public
    /// <summary>
    /// Only instance, you can also customize the instance    /// </summary>
    public static AutoStart Instance { get; private set; } = new AutoStart();

    /// <summary>
    /// Shortcut description, the default value is the current process name    /// </summary>
    public string QuickDescribe { get; set; } = ().ProcessName;

    /// <summary>
    /// The shortcut name, the default value is the current process name    /// </summary>
    public string QuickName { get; set; } = ().ProcessName;

    /// <summary>
    /// Self-start window type, default value is normal window    /// </summary>
    public WshWindowStyle WindowStyle { get; set; } = ;

    /// <summary>
    /// Set automatic startup - just call the method to change the method. The bool variable in the parameter controls the switch that starts the startup, and the default is to enable the self-start startup    /// </summary>
    /// <param name="onOff">Self-Open switch</param>    public void SetAutoStart(bool onOff = true)
    {
        if (onOff)//Start        {
            //Get the path collection of startup path application shortcuts            List&lt;string&gt; shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
            //There are 2 shortcuts, keep one shortcut - avoid more duplication than            if ( &gt;= 2)
            {
                for (int i = 1; i &lt; ; i++)
                {
                    DeleteFile(shortcutPaths[i]);
                }
            }
            else if ( &lt; 1)//Create a shortcut if it does not exist            {
                CreateShortcut(systemStartPath, QuickName, appAllPath, QuickDescribe,WindowStyle);
            }
        }
        else//The power is not started        {
            //Get the path collection of startup path application shortcuts            List&lt;string&gt; shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
            //If there is a shortcut, iterate and delete it all            if ( &gt; 0)
            {
                for (int i = 0; i &lt; ; i++)
                {
                    DeleteFile(shortcutPaths[i]);
                }
            }
        }
        //Create desktop shortcuts - uncomment if needed        //CreateDesktopQuick(desktopPath, QuickName, appAllPath);
    }

    /// &lt;summary&gt;
    /// Create shortcuts on the desktop - call if needed    /// &lt;/summary&gt;
    public void SetDesktopQuick(bool isCreate)
    {
        string desktopPath = ();
        List&lt;string&gt; shortcutPaths = GetQuickFromFolder(desktopPath, appAllPath);
        if (isCreate)
        {
            //Create it without            if ( &lt; 1)
            {
                CreateShortcut(desktopPath, QuickName, appAllPath, QuickDescribe, );
            }
        }
        else
        {
            //Delete if there is            for (int i = 0; i &lt; ; i++)
            {
                DeleteFile(shortcutPaths[i]);
            }
        }
    }
​
    #endregion Public​
    #region Private​
    /// &lt;summary&gt;
    /// Automatically obtain the automatic system startup directory    /// &lt;/summary&gt;
    private string systemStartPath = ();
​
    /// &lt;summary&gt;
    /// Automatically obtain the complete path of the program    /// &lt;/summary&gt;
    private string appAllPath = ().;

    /// &lt;summary&gt;
    /// Automatically get desktop directory    /// &lt;/summary&gt;
    private string desktopPath = ();

    /// &lt;summary&gt;
    /// Create a shortcut to the target path to create a specified file    /// &lt;/summary&gt;
    /// <param name="directory">Target Directory</param>    /// <param name="shortcutName">Shortcut Name</param>    /// <param name="targetPath">File full path</param>    /// <param name="description">Description</param>    /// <param name="iconLocation">Icon Address</param>    /// <returns>Success or failure</returns>    private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description, WshWindowStyle windowStyle, string iconLocation = null)
    {
        try
        {
            //Create if the directory does not exist            if (!(directory)) (directory);
            //Composition path            string shortcutPath = (directory, ("{0}.lnk", shortcutName));
            //If it exists, it will not be created            if ((shortcutPath)) return true;
            //Add a reference Search in Com Windows Script Host Object Model            WshShell shell = new ();
            //Create a shortcut object            IWshShortcut shortcut = ()(shortcutPath);
            //Specify the target path             = targetPath;
            //Set the starting position             = (targetPath);
            //Set the operation mode, default to the regular window             = (int)windowStyle;
            //Setting Notes             = description;
            //Set icon path             = (iconLocation) ? targetPath : iconLocation;
            //Save shortcut            ();
            return true;
        }
        catch (Exception ex)
        {
            string temp = ;
            temp = "";
        }
        return false;
    }

    /// &lt;summary&gt;
    /// Get the shortcut path collection of specified applications under the specified folder    /// &lt;/summary&gt;
    /// <param name="directory">Folder</param>    /// <param name="targetPath">Target application path</param>    /// <returns> Shortcuts for target application</returns>    private List&lt;string&gt; GetQuickFromFolder(string directory, string targetPath)
    {
        List&lt;string&gt; tempStrs = new List&lt;string&gt;();
        ();
        string tempStr = null;
        string[] files = (directory, "*.lnk");
        if (files == null ||  &lt; 1)
        {
            return tempStrs;
        }
        for (int i = 0; i &lt; ; i++)
        {
            //files[i] = ("{0}\{1}", directory, files[i]);
            tempStr = GetAppPathFromQuick(files[i]);
            if (tempStr == targetPath)
            {
                (files[i]);
            }
        }
        return tempStrs;
    }

    /// &lt;summary&gt;
    /// Get the target file path of the shortcut - used to determine whether the automatic startup has been enabled    /// &lt;/summary&gt;
    /// &lt;param name="shortcutPath"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    private string GetAppPathFromQuick(string shortcutPath)
    {
        //Path of shortcut file = @"d:\";        if ((shortcutPath))
        {
            WshShell shell = new WshShell();
            IWshShortcut shortct = (IWshShortcut)(shortcutPath);
            //The path pointed to by the shortcut file.Text = Current shortcut file IWshShortcut class.TargetPath;            //The target directory pointed to by the shortcut file.Text = Current shortcut file IWshShortcut class.WorkingDirectory;            return ;
        }
        else
        {
            return "";
        }
    }

    /// &lt;summary&gt;
    /// Delete file by path - a shortcut to cancel the self-start directory of the program to delete the program from the computer when self-start is enabled    /// &lt;/summary&gt;
    /// <param name="path">path</param>    private void DeleteFile(string path)
    {
        FileAttributes attr = (path);
        if (attr == )
        {
            (path, true);
        }
        else
        {
            (path);
        }
    }
    #endregion Private}

Summarize

In this article, we explore how to use C# language to implement the functionality of an application to automatically run when the system is started, while avoiding the need for administrator privileges. In this way, users can ensure that the application is automatically loaded as the system starts without additional configuration, greatly improving the ease of use and the availability of programs.

at last

This is the article about C# implementing the software startup function (no administrator permission required). For more related content on C# software startup, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!