Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:00:54+00:00 2026-05-23T20:00:54+00:00

I found this code to search for a registry key, it works fine if

  • 0

I found this code to search for a registry key, it works fine if i set it to void and have it pop up a window using messagebox.show , and i see the key it found. but, if i change it to return a string, the compiler fails with “not all code paths return a value” if i try to set a string in the method called “reg_result” and return it, i am returning the value to a string variable that calls the method.

string reg_result2 = Search_For_Registry_Keys(Registry.Users, search);

private static string Search_For_Registry_Keys(RegistryKey rk, string search)
 {
     string reg_result = "";
     if (rk.SubKeyCount > 0)
     {
         foreach (var temp in rk.GetSubKeyNames())
         {
             if (temp.ToLower().Contains(search.ToLower()))
             {
                 reg_result = (String.Format(rk.Name + "\\" + temp));
                 MessageBox.Show(String.Format("Match Found In Registry Key {0} Present At Location {1}", temp, rk.Name + "\\" + temp));
                 return reg_result;

             }
         }
         foreach (var temp in rk.GetSubKeyNames())
         {
             try
             {
                 if (rk.OpenSubKey(temp).SubKeyCount > 0)
                 {
                     Search_For_Registry_Keys(rk.OpenSubKey(temp), search);
                 }
             }
             catch
             {
             }
         }
     }

 }

I then tried the following by putting a 2nd return after the if statement and that got rid of the compiling error about “not all code paths return a value”, but that didn’t seem to help as then the method would never return when i stepped through it until it reached the end of it’s searching, so I’m little lost on how to fix this?? yes i am newbie. I would think i my first code would work and i only need one return for when i find the result i am looking for… 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:00:54+00:00Added an answer on May 23, 2026 at 8:00 pm

    Your code should look like this (comments indicate changes):

    private static string Search_For_Registry_Keys(RegistryKey rk, string search)
     {
         string reg_result = "";
         if (rk.SubKeyCount > 0)
         {
             foreach (var temp in rk.GetSubKeyNames())
             {
                 if (temp.ToLower().Contains(search.ToLower()))
                 {
                     reg_result = (String.Format(rk.Name + "\\" + temp));
                     MessageBox.Show(String.Format("Match Found In Registry Key {0} Present At Location {1}", temp, rk.Name + "\\" + temp));
                     return reg_result;
    
                 }
             }
             foreach (var temp in rk.GetSubKeyNames())
             {
                 try
                 {
                     if (rk.OpenSubKey(temp).SubKeyCount > 0)
                     {
                         // Added and changed lines here
                         string retVal = Search_For_Registry_Keys(rk.OpenSubKey(temp), search);
                         if (!retVal.Equals(""))
                         {
                             return retVal;
                         }
                     }
                 }
                 catch
                 {
                 }
             }
         }
    
         // Added this line
         return reg_result;
     }
    

    Note what I’ve done: if no key is found at all, then you return "" (the value initially assigned to reg_result). Also, when you recurse on a subkey (in the second foreach loop,) and you do find a result, that result is immediately returned.

    Whenever you have a method that promises to return a value, you have to ensure that all code paths through it either a) actually return a value, or b) throw an uncaught exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using NHibernate Search for NHibernate 3.0 GA. I have this code in my
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =
I found this code using Google. private int RandomNumber(int min, int max) { Random
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
I have rewritten the Binary Search Tree code found in the following links to
I found this code at a search on Google: public interface Combine { //
I found this bit of code on stackoverflow at Best practices on displaying search
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
i found this code at activestate, it takes a string and prints permutations of
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.