启动时强制启动应用程序

本文关键字:启动 应用程序 | 更新日期: 2023-09-27 17:52:16

我正在为我的孩子们创建一个类似售货亭的环境。我的应用程序扫描并杀死了很多游戏进程,因为他们还很年轻,不能玩M级或以上级别的游戏,禁用任务管理器,因为他们不需要或不使用它。但我需要一种方法,我可以运行这个应用程序一次,它会复制/添加自己以自动启动。谢谢:(

哦,不,我不想让我的应用程序成为windows服务。

可以轻松编辑注册表或添加到启动文件夹的东西。

启动时强制启动应用程序

这其实很容易做到。这里有两个代码片段,您可以使用它们来完成此操作。这会将程序复制到一个很少访问的文件夹中,然后在计算机启动时使用计算机的注册表将其打开。

注意:我们使用try-and-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 { }
}

这里是添加到启动(我们将文件复制到启动文件夹中,开始按钮>所有程序>启动就是它的位置(

 public static void AddToStartup()
 {
       try
       {
           System.IO.File.Copy(Application.ExecutablePath, Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"'" + "msceInter.exe");
       } 
       catch { }
 }

如果您不想将应用程序作为windows服务运行,那么您可以考虑在HKey_Current_User'Software'Microsoft'Windows'CurrentVersion'Run注册该应用程序这将确保应用程序在启动时执行。

HKEY_LOCAL_MACHINE'SOFTWARE'Microsoft'Windows'CurrentVersion'Run注册应用程序将确保所有用户在启动时都能执行该应用程序。

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

以下是我通常用来自动将应用程序添加到启动环境中的代码。它还包括一小段代码,允许绕过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();
    }
}

如果你只想创建一个到"启动"文件夹的应用程序快捷方式,而不是复制整个文件,请看看这个和这个,因为它不像第一眼看起来那么简单。

  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();
            }
    }

这是一段代码,它将在windows启动时启动。