C# 启动时强制启动应用程序
我正在为我的孩子们创造一个类似信息亭的环境。 我的应用程序扫描并杀死了很多游戏进程,因为它们还很年轻,不能玩M或更高级别的游戏,禁用任务管理器,因为它们不需要或不使用它。 但我需要一种方法,我可以运行这个应用程序一次,它复制/添加自己自动启动。 谢谢:) 哦,不,我不想让我的应用程序成为windows服务C# 启动时强制启动应用程序,c#,C#,我正在为我的孩子们创造一个类似信息亭的环境。 我的应用程序扫描并杀死了很多游戏进程,因为它们还很年轻,不能玩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 { }
}
这里是添加到启动(我们将文件复制到启动文件夹,start Button>All Programs>start-up就是可以找到它的地方)
这其实很容易做到。 这里有两个代码片段可以用来实现这一点。 这会将您的程序复制到一个很少访问的文件夹中,然后在计算机启动时使用计算机的注册表将其打开 注意:我们使用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 { }
}
这里是添加到启动(我们将文件复制到启动文件夹,start Button>All Programs>start-up就是可以找到它的地方)
如果您不想将应用程序运行为Windows服务,那么您可以考虑在“代码”> HKEY-CurrTySuff\\Suth\\Windows \CurrutValue\Run\/Cord>中注册应用程序。 这将确保应用程序在启动时执行 在
HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
上注册应用程序将确保所有用户在启动时执行应用程序
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
registryKey.SetValue("ApplicationName", Application.ExecutablePath);
如果您不想将应用程序运行为Windows服务,那么您可以考虑在“代码”> HKEY-CurrTySuff\\Suth\\Windows \CurrutValue\Run\/Cord>中注册应用程序。 这将确保应用程序在启动时执行 在
HKEY\U LOCAL\U 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();
}
}
使用Microsoft.Win32;
使用制度;
使用系统诊断;
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Security.Principal;
使用System.Windows.Forms;
公共静态类程序
{
[状态线程]
公共静态void Main()
{
如果(!IsAdmin()&&IsWindowsVistaOrHigher())
重新启动();
其他的
AddToStartup(真);
}
私有静态布尔值IsAdmin()
{
WindowsIdentity=WindowsIdentity.GetCurrent();
如果(标识!=null)
return(新WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);
返回false;
}
私有静态布尔值IsWindowsVistaOrHigher()
{
OperatingSystem os=Environment.OSVersion;
返回((os.PlatformID.Win32NT)=&(os.Version.Major>=6));
}
私有静态void AddToStartup(布尔targetEveryone)
{
尝试
{
Environment.SpecialFolder=((targetEveryone&&IsWindowsVistaOrHigher())?Environment.SpecialFolder.CommonStartup:Environment.SpecialFolder.Startup);
字符串fileDestination=Path.Combine(Environment.GetFolderPath(文件夹),Path.GetFileName(Application.ExecutablePath));
如果(!File.Exists(fileDestination))
File.Copy(Application.ExecutablePath,fileDestination);
}
捕获{}
尝试
{
使用(RegistryKey main=(targetEveryone?Registry.LocalMachine:Registry.CurrentUser))
{
使用(RegistryKey=main.OpenSubKey(“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,true))
{
String fileName=Path.GetFileNameWithoutExtension(Application.ExecutablePath);
if(key.GetValue(fileName)==null)
key.SetValue(文件名,Application.ExecutablePath);
}
}
}
捕获{}
}
私有静态无效重新启动()
{
String[]argumentsArray=Environment.GetCommandLineArgs();
String argumentsLine=String.Empty;
对于(Int32 i=1;i
如果您只想创建一个到启动文件夹的应用程序快捷方式,而不是复制整个文件,请看一看,因为这并不像乍一看那么简单。以下是我通常用于将应用程序自动添加到启动环境的代码。它还包括一小段代码,允许绕过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();
}
}
使用Microsoft.Win32;
使用制度;
使用系统诊断;
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Security.Principal;
使用System.Windows.Forms;
公共静态类程序
{
[状态线程]
公共静态void Main()
{
如果(!IsAdmin()&&IsWindowsVistaOrHigher())
重新启动();
其他的