Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have a directory structure inside Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services like this :

MYService
MYService-1
MYService-2
MYService-3

There would be other default service from Microsoft will be present as well.
while searching for the my MYService I don't want to iterate all the services present under Services folder but only my services like MYService
MYService-1, MYService-2 etc.. then I can decide which is best suitable MYService match by comparing the ImagePath of the MYService and incoming servicepath.

I have the code for the second part (matching ImagePath).
Could someone help me for the first part.

What I have tried:

I have searched for this code, but it iterating all services :
Registry.LocalMachine.OpenSubKey(REGISTRY_CONTROLSET_SERVICES)
Posted
Updated 15-Dec-20 0:50am
v2
Comments
CHill60 15-Dec-20 3:51am    
Just match the start of the name. Show the code you are using to "match" the ImagePath - it's probably going to be very very similar

 
Share this answer
 
Comments
Premchand Kumar 15-Dec-20 5:35am    
Hey Richard, Thanks for the information,
but I have a condition already like below :
foreach (string instanceName in services.GetSubKeyNames()) { }
but in this case I have to iterate on all keys present under services.

What I want is go to very first directory key which name starts with MYService, and then start iterating.
Richard MacCutchan 15-Dec-20 6:37am    
You can only iterate what the system provides, as shown in the documentation.
Premchand Kumar 16-Dec-20 1:28am    
right, I wanted to discover if this is possible to hide other services from end users when watching from ProcessMonitor.
Richard MacCutchan 16-Dec-20 4:04am    
You have no real control over what users can and cannot do on their own systems; don't waste time trying. It is much better to focus your efforts on providing efficient, high quality, solutions.
In response to your comment, here are a couple of suggestions (N.B. not tested, or even compiled)
C#
foreach (string instanceName in services.GetSubKeyNames()) 
{ 
     if !instanceName.Contains("MYService") continue; //moves to next iteration

}

Alternatively you could put the array of subkeynames into a list keylist, extract from that list only the MYService entries and iterate through that
C#
var myservices = keylist.Where(t => uids.Contains("MyService"));
foreach (var instanceName in myservices)
{
   // do your stuff
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 15-Dec-20 7:35am    
I (foolishly) assumed that OP could figure out that part.
CHill60 15-Dec-20 13:16pm    
You have just made me laugh out loud! Going into the registry with so little knowledge - glad OP doesn't work at my place!
Premchand Kumar 16-Dec-20 5:07am    
I tried this code ::
var keylist = services.GetSubKeyNames();
var myservices = keylist.Where(t => keylist.Contains("MYService"));

foreach (var instance in myservices)
{
Console.WriteLine("currentInstance : " + instance);
}

instead of finding directory specific to MYService, it is iterating to all entries under services strange..
CHill60 16-Dec-20 8:01am    
Strange indeed. I get a compilation error 'System.Array' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?). GetSubKeyNames() returns a string array, you need to "put the array of subkeynames into a list keylist" - as I said in my solution. If you are still getting all entries then you are running the results of your previous compile and not this one
Premchand Kumar 17-Dec-20 2:35am    
Here is my full code in order to avoid any confusion about the sol. you said :

RegistryKey services = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services");
string []keyList = services.GetSubKeyNames();
var myservices = keyList.Where(t => keyList.Contains("MYService"));
foreach (var instanceName in myservices)
{
Console.WriteLine("current instanceName is : " + instanceName);
}
with this solution I am getting all instances name from services in variable instanceName.
Have checked the service MYService is present in Services registry.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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