0

The below if statement never resolves to true when the registry key isn't found. What am I missing here?

if ((Convert.ToString(Registry.GetValue(key, "EnableLinkedConnections", "")) == "0") ||
    (Convert.ToString(Registry.GetValue(key, "EnableLinkedConnections", "")) == null))
Ben Barden
  • 2,001
  • 2
  • 20
  • 28
JimDel
  • 4,309
  • 11
  • 54
  • 99
  • What is the return value of Registry.GetValue in the debugger? – StingyJack Nov 21 '12 at 15:53
  • 2
    Also, you should be using something that could never be in a registry key when testing for existence in this way. A reg key could very likely have an empty string in it, rendering this test ineffective. – StingyJack Nov 21 '12 at 16:03

4 Answers4

8

Per the MSDN Docs, the 3rd parameter you are passing in is the value that is returned to you when the key is not found.

Since you are specifying "" (String.Empty) - that is the value you should be checking for, not null or "0".

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • Good point. My response still holds for the case where the third parameter is not specified (and provides the correct behavior), but you're right in that the third parameter overrides that one way or the other. – Ben Barden Nov 21 '12 at 16:03
5

Convert.ToString on null as object returns "". Try string.IsNullOrEmpty().

Ben Barden
  • 2,001
  • 2
  • 20
  • 28
  • 1
    The string representation of value, or String.Empty if value is null. http://msdn.microsoft.com/en-us/library/astxcyeh.aspx – Gavin Nov 21 '12 at 15:55
  • or String.IsNullOrWhitespace() if its available/preferred. – StingyJack Nov 21 '12 at 15:57
  • @Gavin : Convert.ToString(null) returns null. Convert.ToString(null as Object) returns "". http://stackoverflow.com/questions/10355736/why-does-convert-tostringnull-return-a-different-value-if-you-cast-null and Registry.GetValue returns as object. – Ben Barden Nov 21 '12 at 15:58
  • @StingyJack it's a bit more thorough, but in this case we know what the answer will be - technically we could instead use `== ""` safely (and likely ever so slightly faster) – Ben Barden Nov 21 '12 at 16:00
  • But in this case the Convert.ToString method will never received a null object as @StingyJack noticed. The right answer is a combination of both yours. – Renaud Dumont Nov 21 '12 at 16:01
2

Because Registry.GetValue is returning a null object, which Convert.ToString() converts to the empty string (see here) which you are not checking for. Try this instead:

object value = Registry.GetValue(key, "EnableLinkedConnections", "");
if (value != null)
{
    string text = Convert.ToString(value);
} 
Community
  • 1
  • 1
Polyfun
  • 9,479
  • 4
  • 31
  • 39
0

As a rule of thumb, please check before Convert.ToString if it is null, store it in object before calling two times the same method this will increase performance.

object regystryValue = Registry.GetValue(key, "EnableLinkedConnections", "");

if(regystryValue != null && regystryValue.ToString() =="0"){
//do things
}
Alberto León
  • 2,879
  • 2
  • 25
  • 24
  • That's not the question that was asked; this doesn't actually answer the question, even if it's correct. – Servy Nov 21 '12 at 16:01