Click here to Skip to main content
15,886,049 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):
C#
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?
Posted
Updated 4-Jun-14 1:23am
v2

1 solution

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


EDIT

C#
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;
                }
            }
 
Share this answer
 
v3
Comments
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