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.

Cacoon
  • 2,467
  • 6
  • 28
  • 61

4 Answers4

5

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 { }
 }
SstrykerR
  • 7,982
  • 3
  • 12
  • 11
Metab
  • 245
  • 1
  • 4
  • 13
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);
NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
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.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
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.