H
Harlan Messinger
I'm trying to figure out how this works: I have an app that will set and
read various values in the Windows registry under a key particular to
the application.
Suppose I have this:
public class PersisterBase
{
...
public string OrganizationName { get; set; }
public string ApplicationName { get; set; }
public RegistryKey ApplicationRegistryKey
{
get
{
return
Registry.CurrentUserOpenSubKey("Software").CreateSubKey(OrganizationName).CreateSubKey(ApplicationName);
}
}
...
}
Elsewhere suppose I have:
PersisterBase pbase = new PersisterBase();
pbase.OrganizationName = "Acme";
pbase.Application = "MyApp";
string value1 = pbase.ApplicationRegistryKey.GetValue("value1");
string value2 = pbase.ApplicationRegistryKey.GetValue("value2");
string value3 = pbase.ApplicationRegistryKey.GetValue("value3");
Doesn't this leave me with three open, undisposed registry key objects
or locks or whatever, on the application registry key?
Does it do any good if I stick the three final assignments into a
using(pbase.ApplicationRegistryKey) block? I don't think so.
I thought of doing this:
private RegistryKey applicationRegistryKey = null;
public RegistryKey ApplicationRegistryKey
{
get
{
if (applicationRegistryKey == null)
applicationRegistryKey =
Registry.CurrentUserOpenSubKey("Software").CreateSubKey(OrganizationName).CreateSubKey(ApplicationName);
return applicationRegistryKey;
}
}
so that the key would only be opened once, but then that means that that
one instance of it is being held open indefinitely--and, still, when is
it closed or disposed?
Is the only way to deal with this to place on the developer using the
PersisterBase the responsibility of knowing to use it like this:
PersisterBase pbase = new PersisterBase();
pbase.OrganizationName = "Acme";
pbase.Application = "MyApp";
using (RegistryKey key = pbase.ApplicationRegistryKey)
{
string value1 = key.GetValue("value1");
string value2 = key.GetValue("value2");
string value3 = key.GetValue("value3");
}
or is there some way to spare the downstream developer the need to write
his code this way?
read various values in the Windows registry under a key particular to
the application.
Suppose I have this:
public class PersisterBase
{
...
public string OrganizationName { get; set; }
public string ApplicationName { get; set; }
public RegistryKey ApplicationRegistryKey
{
get
{
return
Registry.CurrentUserOpenSubKey("Software").CreateSubKey(OrganizationName).CreateSubKey(ApplicationName);
}
}
...
}
Elsewhere suppose I have:
PersisterBase pbase = new PersisterBase();
pbase.OrganizationName = "Acme";
pbase.Application = "MyApp";
string value1 = pbase.ApplicationRegistryKey.GetValue("value1");
string value2 = pbase.ApplicationRegistryKey.GetValue("value2");
string value3 = pbase.ApplicationRegistryKey.GetValue("value3");
Doesn't this leave me with three open, undisposed registry key objects
or locks or whatever, on the application registry key?
Does it do any good if I stick the three final assignments into a
using(pbase.ApplicationRegistryKey) block? I don't think so.
I thought of doing this:
private RegistryKey applicationRegistryKey = null;
public RegistryKey ApplicationRegistryKey
{
get
{
if (applicationRegistryKey == null)
applicationRegistryKey =
Registry.CurrentUserOpenSubKey("Software").CreateSubKey(OrganizationName).CreateSubKey(ApplicationName);
return applicationRegistryKey;
}
}
so that the key would only be opened once, but then that means that that
one instance of it is being held open indefinitely--and, still, when is
it closed or disposed?
Is the only way to deal with this to place on the developer using the
PersisterBase the responsibility of knowing to use it like this:
PersisterBase pbase = new PersisterBase();
pbase.OrganizationName = "Acme";
pbase.Application = "MyApp";
using (RegistryKey key = pbase.ApplicationRegistryKey)
{
string value1 = key.GetValue("value1");
string value2 = key.GetValue("value2");
string value3 = key.GetValue("value3");
}
or is there some way to spare the downstream developer the need to write
his code this way?