简体   繁体   English

在启动时强制启动应用程序

[英]Force application launch on startup

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. 我的应用程序扫描并杀死了很多游戏进程,因为他们还很小,就无法玩M级或更高等级的游戏,由于不需要或不使用它而禁用了任务管理器。 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. 哦,不,我不想使我的应用程序成为Windows服务。

Something that could edit the registry or add to startup folder easily. 可以编辑注册表或轻松添加到启动文件夹的内容。

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. 注意 :我们使用try和catch语句只是为了以防万一,您应该始终使用它们。

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 { }
 }

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. 如果您不想将应用程序作为Windows服务运行,则可以考虑在HKey_Current_User\\Software\\Microsoft\\Windows\\CurrentVersion\\Run注册该应用程序。这将确保该应用程序将在启动时执行。

Registering the application at HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run will ensure that the application executes at startup for all users. HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run注册该应用程序将确保该应用程序在启动时为所有用户执行。

RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
registryKey.SetValue("ApplicationName", Application.ExecutablePath);

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. 它还包括一小段代码,可绕过UAC保护。

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. 如果您只想创建一个到Startup文件夹的应用程序快捷方式,而不是复制整个文件,请看一下 一点,因为它并不像乍看起来那样简单。

  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. 这是一些代码,它将在Windows启动时启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM