2013-01-11 40 views
1

我正在为我的孩子创建一个类似环境的自助服务终端。 我的应用程序可以扫描并杀死很多游戏进程,因为它们很年轻时无法玩M级或以上级别的游戏,所以禁用任务管理器,因为他们没有必要或不需要使用它。 但我需要一种方式,我可以运行这个应用程序一次,它复制/添加自己启动自动。 谢谢:)启动时强制应用程序启动

哦,不,我不想让我的应用程序一个Windows服务。

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

+1

http://stackoverflow.com/questions/7608225/autorun-the-application-using-c-sharp – chameleon86

+1

似乎更容易只是把它放在启动文件夹自己,而不是把它检查每次运行时间。 – NominSim

+0

我给你提供了一个用于你的程序的基本片段,祝你好运。 – Metab

回答

4

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

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

这里也加入到启动(我们我们的文件复制到启动文件夹,开始按钮>所有程序>启动就是它会被发现)

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

是不是空的全球渔获声明是一个非常糟糕的做法? – SepehrM

4

如果你不不想将应用程序作为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); 
2

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

如果你想创建,而不是复制整个文件只是一个应用程序快捷方式到启动文件夹,在thisthis看一看,因为它不是简单,因为它可能看起来乍一看。

+0

“绕过UAC保护”是什么意思? – SepehrM

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

这是一段代码,它会在Windows启动时启动它。

相关问题