When I try to read my settings I get an error. I know I write wrong somewhere. the procedure to be done alone as I couldn't find much information.
//Settings read code
public static class proSave
{
public static propertyClass oku
{
get
{
XmlSerializer serialize = new XmlSerializer(typeof(propertyClass));
var stream = new StreamReader("settings.xml");
return (propertyClass)serialize.Deserialize(stream);
}
}
}
setting apply code
namespace property
{
[Serializable]
public class propertyClass
{
private string _serverName = proSave.oku.serverName;
[Description("Server Bağlantı Adı"), Category("Server Setting")]
public string serverName { get { return _serverName; }}
private string _databaseName = proSave.oku.serverName;
[Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")]
public string databaseName { get { return _serverName; } }
private string _user = proSave.oku.user;
[Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")]
public string user { get { return _user; } }
private string _password = proSave.oku.password;
[Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")]
public string password { get { return _password; } }
private int _gridHeight = proSave.oku.gridHeight;
[Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")]
public int gridHeight { get { return _gridHeight; } }
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<propertyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<serverName>1</serverName>
<databaseName>Kadir</databaseName>
<user>as</user>
<gridHeight>40</gridHeight>
</propertyClass>
I think you have a circular reference in your propertyClass which is causing a loop which eventually results in a StackOverflow exception (referencing proSave.oku, which is an instance of the propertyClass, within the propertyClass). Removing this reference in your propertyClass fixes your issue - the rest of your code can be left as it is:
[Serializable]
public class propertyClass
{
[Description("Server Bağlantı Adı"), Category("Server Setting")]
public string serverName { get; set; }
[Description("Server Bağlantısı Dosya Adı"), Category("Server Setting")]
public string databaseName { get; set; }
[Description("Server Bağlantısı kullanıcı adı"), Category("Server Setting")]
public string user { get; set; }
[Description("Server Bağlantısı kullanıcı şifresi"), Category("Server Setting")]
public string password { get; set; }
[Description("Grid Hücre Yükseklik Ayarı \nFormlar Yenilendiğinde etkinleştirilecektir."), Category("Grid Ayarları")]
public int gridHeight { get; set; }
}
If you wanted to extend the implementation to prevent reading the xml file each time, you could further modify your proSave class to "cache" the settings, for example:
public static class proSave
{
private static propertyClass settings;
private const string SettingsFilePath = "C:\\settings.xml";
public static propertyClass oku
{
get
{
if (settings == null)
{
settings = GetSettings();
return settings;
}
return settings;
}
}
public static void SaveSettings(propertyClass settings)
{
XmlSerializer writer = new XmlSerializer(typeof(propertyClass));
using (FileStream file = File.Create(SettingsFilePath))
{
writer.Serialize(file, settings);
}
}
private static propertyClass GetSettings()
{
XmlSerializer serialize = new XmlSerializer(typeof(propertyClass));
using (var stream = new StreamReader(SettingsFilePath))
{
return (propertyClass)serialize.Deserialize(stream);
}
}
}
You would need to handle IOExceptions if the file was locked when writing the settings back in the SaveSettings() method.
You could also look into alternatives, such as using the built-in ConfigurationManager or implementing a custom ConfigurationSection. Hope this helps.