如何在.NET中获取启动应用程序?
CurrentUser LocalMachine SOFTWARE\Microsoft\Windows\CurrentVersion\Run 我从这里获得了启动应用程序,但没有得到预期的结果,因为我的计算机上有 19 个启动应用程序,我需要所有启动应用程序。
Windows 有多个目录和注册表项,程序在启动时启动:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
- 启动时启动特定于当前用户的程序
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
- 程序在系统范围内为所有用户启动时启动
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
- 与 HKEY_CURRENT_USER\ ... \Run
类似,但程序仅运行一次并在下次登录时删除该条目
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
- 与 HKEY_LOCAL_MACHINE\ ... \Run
类似,不同之处在于程序仅运行一次并在下次登录时删除该条目
还有这些目录:
%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Startup
- 在启动时为所有用户在系统范围内运行的启动程序
%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
- 启动时仅为当前用户运行的启动程序
我们必须查询注册表值并从
...Programs\Startup
目录中获取文件。
示例:
using System.IO;
using Microsoft.Win32;
public static class StartupApps
{
private static readonly string[] s_startupRegKeys =
[
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce",
@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run",
@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce"
];
private static IEnumerable<string> ReadRegSzValues(string keyPath)
{
string actualKey =
keyPath.Contains("HKEY_LOCAL_MACHINE")
? keyPath.Split(["HKEY_LOCAL_MACHINE"], StringSplitOptions.None)[1]
: keyPath.Split(["HKEY_CURRENT_USER"], StringSplitOptions.None)[1];
bool usesLocalMachine = keyPath.Contains("HKEY_LOCAL_MACHINE");
RegistryKey? key =
usesLocalMachine
? Registry.LocalMachine.OpenSubKey(actualKey)
: Registry.CurrentUser.OpenSubKey(actualKey);
if (key == null)
{
yield break;
}
foreach (string valueName in key.GetValueNames())
{
object value = key.GetValue(valueName);
if (value is string stringValue)
{
yield return stringValue;
}
}
key.Dispose();
}
/// <summary>
/// Return startup apps stored in the Windows registry.
/// </summary>
/// <returns>A list of startup apps from Windows registry.</returns>
public static IEnumerable<string> GetStartupAppsFromRegistry()
{
foreach (string key in s_startupRegKeys)
{
foreach (string regKey in ReadRegSzValues(key))
{
yield return regKey;
}
}
}
public static IEnumerable<string> GetStartupAppsFromStartupDirectory()
{
foreach (string startupObject in Directory.GetFiles(GetCurrentUserStartupDirectory()))
{
yield return startupObject;
}
foreach (string startupObject in Directory.GetFiles(GetSystemWideStartupDirectory()))
{
yield return startupObject;
}
}
private static string GetCurrentUserStartupDirectory()
{
string? appData = Environment.GetEnvironmentVariable("APPDATA");
if (appData is null)
{
throw new InvalidOperationException("No APPDATA variable was found. Make sure the operating system is Windows.");
}
return Path.Combine(appData, "Microsoft\\Windows\\Start Menu\\Programs");
}
private static string GetSystemWideStartupDirectory()
{
string? programData = Environment.GetEnvironmentVariable("ALLUSERSPROFILE")
?? Environment.GetEnvironmentVariable("PROGRAMDATA");
if (programData is null)
{
throw new InvalidOperationException("No ALLUSERSPROFILE or PROGRAMDATA variable was found. Make sure the operating system is Windows.");
}
return Path.Combine(programData, "Microsoft\\Windows\\Start Menu\\Programs");
}
}
像这样使用它:
IEnumerable<string> startupObjects1 = StartupObjects.GetStartupAppsFromStartupDirectory();
IEnumerable<string> startupObjects2 = StartupObjects.GetStartupAppsFromRegistry();