Click here to Skip to main content
15,887,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have multiple registry entries all beginning with the same prefix but I only want to get the first record so I can read the EventMessageFile Details. So far I have this which gives me a count of the records:
C#
int count = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames().Where(s => s.StartsWith("CardSpace")).Count();


how do I select the first record to read the details?
Posted
Comments
Prasad Avunoori 3-Jun-14 6:20am    
Use FirstOrDefault()

You have to use:
C#
Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames().Where(s => s.StartsWith("CardSpace")).FirstOrDefault();
 
Share this answer
 
Comments
pmcm 4-Jun-14 6:56am    
I was able to get this working in a test app that I developed, but when I copied the logic to my main application:
string serviceName = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames().Where(s => s.StartsWith(editedServiceName)).FirstOrDefault();

I'm getting this error: "Object reference not set to an instance of an object." The variable editedServiceName is valid and the registry exists (I checked it manually). This application is built using v2 of .Net Framework. Can you think of any other reason why this is failing over at this point?
Thanks to both you guys I've ended up with this:
C#
string servicename = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\").GetSubKeyNames().Where(s => s.StartsWith("CardSpace")).FirstOrDefault();

string dotNetFourPath = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" + servicename; // "AllstateCTSNGMedbillEP"; //servicename here
            using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(dotNetFourPath))
            {
                Console.WriteLine(registryKey.GetValue("EventMessageFile")); //returns EventMessageFile - Value Data

                string sDt = registryKey.GetValue("EventMessageFile").ToString();
                //sDt.Split('\\');                
                string[] sVar = sDt.Split('\\');
                string sYourValue = sVar[4];
                Console.WriteLine(sVar[0]);
                Console.WriteLine(sVar[1]);
                Console.WriteLine(sVar[2]);
                Console.WriteLine(sVar[3]);
                Console.WriteLine(sVar[4]);
                Console.WriteLine(sVar[5]);
                Console.WriteLine(sYourValue);
            }
 
Share this answer
 

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