1

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, disable task manager as they have no need or use for it. But i need a way i can run this application once and it copys/adds itself to start up automatically. Thanks :)

Oh and no, i dont want to make my application a windows service.

Something that could edit the registry or add to startup folder easily.

3
  • 1
    stackoverflow.com/questions/7608225/…
    – chameleon
    Commented Jan 11, 2013 at 14:31
  • 1
    Seems easier just to put it in startup folder yourself, rather than have it check every time it is run.
    – NominSim
    Commented Jan 11, 2013 at 14:31
  • I gave you hopfully a basic snippet to use for your program, good luck.
    – Metab
    Commented Jan 11, 2013 at 14:35

4 Answers 4

5
+50

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 { }
 }
1
  • 2
    Isn't empty global catch statement a very bad practice?
    – SepehrM
    Commented Nov 4, 2014 at 17:20
4

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

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.

1
  • What do you mean by "bypass UAC protection"?
    – SepehrM
    Commented Nov 4, 2014 at 17:21
0
  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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.