Click here to Skip to main content
15,791,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

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?
Updated 4-Jun-14 2:23am

1 solution

That code is using LINQ, which by default is not in 2.0 .Net framework


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;
Share this answer
pmcm 4-Jun-14 7:34am    
Can u suggest a workaround for me?
Pikoh 4-Jun-14 7:36am    
See improved solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900