Force application launch on startup

前端 未结 4 555
萌比男神i
萌比男神i 2021-01-01 07:52

I am creating a kiosk like environment for my kids. My application scans and kills alot of game processes as they cannot play M or above rated games as they are quite young,

相关标签:
4条回答
  • 2021-01-01 08:15

    If you don't want to run the application as a windows service, then you can consider registering the application at HKey_Current_User\Software\Microsoft\Windows\CurrentVersion\Run This will ensure that the application will execute at startup.

    Registering the application at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run will ensure that the application executes at startup for all users.

    RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    registryKey.SetValue("ApplicationName", Application.ExecutablePath);
    
    0 讨论(0)
  • 2021-01-01 08:16

    That's actually quite easy to do. Here are two code snippets you can use to do that. This copies your program into a folder that is rarely accessed then uses the computer's registry to open it on the computer's startup.

    Note: We use try and catch statements just in case, you should always use them.

    public static void AddToRegistry()
    {
           try
           {
               System.IO.File.Copy(Application.ExecutablePath, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ATI\" + "msceInter.exe");
               RegistryKey RegStartUp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
               RegStartUp.SetValue("msceInter", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ATI\" + "msceInter.exe");
           }
           catch { }
    }
    

    Here is adding to start up (We copy our file into the startup folder, Start Button > All Programs > Start Up is where it will be found)

     public static void AddToStartup()
     {
           try
           {
               System.IO.File.Copy(Application.ExecutablePath, Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\" + "msceInter.exe");
           } 
           catch { }
     }
    
    0 讨论(0)
  • 2021-01-01 08:25
      using Microsoft.Win32;
    
    
    
        public partial class Form1 : Form
                {
    
    
                RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                public Form1()
                {
                    reg.SetValue("AutoRun", Application.ExecutablePath.ToString());
                    InitializeComponent();
                }
        }
    

    This is a bit of code that will make it start when windows starts.

    0 讨论(0)
  • 2021-01-01 08:28

    Here is the code I normally use to automatically add applications to startup environment. It also includes a small piece of code that allows to bypass UAC protection.

    using Microsoft.Win32;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Windows.Forms;
    
    public static class Program
    {
        [STAThread]
        public static void Main()
        {
            if (!IsAdmin() && IsWindowsVistaOrHigher())
                RestartElevated();
            else
                AddToStartup(true);
        }
    
        private static Boolean IsAdmin()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
    
            if (identity != null)
                return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);
    
            return false;
        }
    
        private static Boolean IsWindowsVistaOrHigher()
        {
            OperatingSystem os = Environment.OSVersion;
            return ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 6));
        }
    
        private static void AddToStartup(Boolean targetEveryone)
        {
            try
            {
                Environment.SpecialFolder folder = ((targetEveryone && IsWindowsVistaOrHigher()) ? Environment.SpecialFolder.CommonStartup : Environment.SpecialFolder.Startup);
                String fileDestination = Path.Combine(Environment.GetFolderPath(folder), Path.GetFileName(Application.ExecutablePath));
    
                if (!File.Exists(fileDestination))
                    File.Copy(Application.ExecutablePath, fileDestination);
            }
            catch { }
    
            try
            {
                using (RegistryKey main = (targetEveryone ? Registry.LocalMachine : Registry.CurrentUser))
                {
                    using (RegistryKey key = main.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                    {
                        String fileName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
    
                        if (key.GetValue(fileName) == null)
                            key.SetValue(fileName, Application.ExecutablePath);
                    }
                }
            }
            catch { }
        }
    
        private static void RestartElevated()
        {
            String[] argumentsArray = Environment.GetCommandLineArgs();
            String argumentsLine = String.Empty;
    
            for (Int32 i = 1; i < argumentsArray.Length; ++i)
                argumentsLine += "\"" + argumentsArray[i] + "\" ";
    
            ProcessStartInfo info = new ProcessStartInfo();
            info.Arguments = argumentsLine.TrimEnd();
            info.FileName = Application.ExecutablePath;
            info.UseShellExecute = true;
            info.Verb = "runas";
            info.WorkingDirectory = Environment.CurrentDirectory;
    
            try
            {
                Process.Start(info);
            }
            catch { return; }
    
            Application.Exit();
        }
    }
    

    If you want to create just an application shortcut to the Startup folder instead of copying the whole file, take a look at this and this as it's not as simple as it might look at a first glance.

    0 讨论(0)
提交回复
热议问题