когда для Perperty создано приватное поле, является ли оно обязательным ??
а когда не создали?
enter code here
namespace ApplicationStartSample
{
public class Configuration
{
private Configuration()
{
}
private static Configuration _Current;
public static Configuration Current
{
get
{
if (_Current == null)
_Current = new Configuration();
return _Current;
}
}
private const string Path = "Software\\MFT\\Registry Sample";
public bool EnableWelcomeMessage
{
get
{
return bool.Parse(Read("EnableWelcomeMessage", "false"));
}
set
{
Write("EnableWelcomeMessage", value.ToString());
}
}
public string Company //why do not create private field?
{
get
{
return Read("Company", "MFT");
}
set
{
Write("Company", value);
}
}
public string WelcomeMessage
{
get
{
return Read("WelcomeMessage", string.Empty);
}
set
{
Write("WelcomeMessage", value);
}
}
public string Server
{
get
{
return Read("Server", ".\\Sqldeveloper");
}
set
{
Write("Server", value);
}
}
public string Database
{
get
{
return Read("Database", "Shop2");
}
set
{
Write("Database", value);
}
}
private static string Read(string name, string @default)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, false);
if (key == null)
return @default;
try
{
string result = key.GetValue(name).ToString();
key.Close();
return result;
}
catch
{
return @default;
}
}
private static void Write(string name, string value)
{
try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, true);
if (key == null)
key = Registry.CurrentUser.CreateSubKey(Path);
key.SetValue(name, value);
key.Close();
}
catch
{
}
}
}
}