从RegistryKey中选择FirstOrDefault() [英] Select FirstOrDefault() from RegistryKey
问题描述
我能够在开发的测试应用程序(V4.5 .Net Framework)中使用以下代码段,但是当我将逻辑复制到主应用程序(V2.0 .Net Framework)时:
I was able to get the following snippet working in a test app (V4.5 .Net Framework) that I developed, but when I copied the logic to my main application (V2.0 .Net Framework):
string serviceName = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames().Where(s => s.StartsWith(editedServiceName)).FirstOrDefault();
string registryKeyPath = @"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" + serviceName;
我收到此错误:对象引用未设置为对象的实例."即serviceName = null
变量editedServiceName有效且注册表存在(我手动对其进行了检查). .Net Framework中的差异是否导致此错误?还是其他原因导致此操作现在无法进行故障转移?
I''m getting this error: "Object reference not set to an instance of an object." i.e. serviceName = null
The variable editedServiceName is valid and the registry exists (I checked it manually). Is the differences in the .Net Framework causing this error? Or what other reason is causing this to fail over at this point?
推荐答案
该代码使用的是LINQ,默认情况下不在2.0 .Net framework中. >
编辑
That code is using LINQ, which by default is not in 2.0 .Net framework
EDIT
string registryKeyPath;
string[] serviceNames = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames();
foreach (string serviceName in serviceNames)
{
if (serviceName.StartsWith(editedServiceName)
{
registryKeyPath = @"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" + serviceName;
break;
}
}
这篇关于从RegistryKey中选择FirstOrDefault()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!