My question is very simple. How can you store an double array in the settings of the program, and getting it out of it.
I already got this:
string[,] user_credits = new string[user_credits_array, 10];
user_credits[new_user_id, 0] = user_name;
user_credits[new_user_id, 1] = user_email;
user_credits[new_user_id, 2] = user_acc_name;
user_credits[new_user_id, 3] = user_acc_pass;
user_credits[new_user_id, 4] = sSelectedClient;
user_credits[new_user_id, 5] = server_inkomend;
user_credits[new_user_id, 6] = server_uitgaand;
user_credits[new_user_id, 7] = server_port + "";
user_credits[new_user_id, 8] = ssl_state;
So as you can see, am i using the id of the user to store the info together. And I'm storing it this way:
Properties.Settings.Default.user_credits = user_credits;
Properties.Settings.Default.Save();
Am I doing it right? is now the array still in the user settings?
And how can I get it out of it (the settings with the right user id)?
I know it might sound crazy, but i think this is the best way. But if you guys know an better way, tell me. I
Edit 1:
I've got this piece of code:
string[,] user_credits = new string[user_credits_array, 10];
user_credits[new_user_id, 0] = user_name;
user_credits[new_user_id, 1] = user_email;
user_credits[new_user_id, 2] = user_acc_name;
user_credits[new_user_id, 3] = user_acc_pass;
user_credits[new_user_id, 4] = sSelectedClient;
user_credits[new_user_id, 5] = server_inkomend;
user_credits[new_user_id, 6] = server_uitgaand;
user_credits[new_user_id, 7] = server_port + "";
user_credits[new_user_id, 8] = ssl_state;
MySettings settingsTest = new MySettings();
settingsTest.Save(MySettings.GetDefaultPath());
MySettings anotherTest = MySettings.Load(MySettings.GetDefaultPath());
And after running the code, this is how the XML file looks like:
<Complex name="Root" type="WeProgram_Mail.MySettings, WeProgram_Mail, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<Properties>
<Null name="user_credits" />
</Properties>
Now I don't understand why the array isn't saved. Because I've got this line
public string[,] user_credits { get; set; }
And I thought that would pick up the usersettings from the array, but somehow they don't.
Ah, I see the problem.
As you see in the XML file, the array in the MySettings instance(settingsTest) is null.
That is because you fill the array outside the settingsTest object and never touch or initialize settingsTest.user_credits...
Try the following:
MySettings settingsTest = new MySettings();
settingsTest.user_credits = new string[user_credits_array, 10];
settingsTest.user_credits[new_user_id, 0] = user_name;
settingsTest.user_credits[new_user_id, 1] = user_email;
settingsTest.user_credits[new_user_id, 2] = user_acc_name;
settingsTest.user_credits[new_user_id, 3] = user_acc_pass;
settingsTest.user_credits[new_user_id, 4] = sSelectedClient;
settingsTest.user_credits[new_user_id, 5] = server_inkomend;
settingsTest.user_credits[new_user_id, 6] = server_uitgaand;
settingsTest.user_credits[new_user_id, 7] = server_port + "";
settingsTest.user_credits[new_user_id, 8] = ssl_state;
settingsTest.Save(MySettings.GetDefaultPath());
MySettings anotherTest = MySettings.Load(MySettings.GetDefaultPath());
Use System.Collections.Specialized.StringCollection
for setting and add a XML String (contain your additional properties like 'user_name' or 'user_email') to each string:
var collection = new StringCollection {"<user_name>aaaa<user_name><user_email>asdfasd@asdfasd</user_email>"};
Properties.Settings.Default.MySetting = collection;
Properties.Settings.Default.Save();
and parse XML when you need properties.
Well, usually I just use http://www.sharpserializer.com/en/index.html
It is childishly easy to use, fast, and can serialize more or less anything, including Dictionary, etc.
Nice thing is, that you can serialize to multiple target formats like binary.
EDIT: Example for Serialization with SharpSerializer.
Have not compiled the code, but should be fine.
Drawback: A property to be stored has to be public...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Polenter.Serialization;
namespace Test
{
public class MySettings
{
// this is a property we want to serialize along with the settings class.
// the serializer will automatically recognize it and serialize/deserialize it.
public string[,] user_credits { get; set; }
//
public static MySettings Load(string path)
{
if (!System.IO.File.Exists(path)) throw new System.ArgumentException("File \"" + path + "\" does not exist.");
try
{
MySettings result = null;
// the serialization settings are just a needed standard object as long as you don't want to do something special.
SharpSerializerXmlSettings settings = new SharpSerializerXmlSettings();
// create the serializer.
SharpSerializer serializer = new SharpSerializer(settings);
// deserialize from File and receive an object containing our deserialized settings, that means: a MySettings Object with every public property in the state that they were saved in.
result = (MySettings)serializer.Deserialize(path);
// return deserialized settings.
return result;
}
catch (Exception err)
{
throw new InvalidOperationException(string.Format("Error in MySettings.LoadConfiguration():\r\nMessage:\r\n{0}\r\nStackTrace:\r\n{1}", err.Message, err.StackTrace), err);
}
}
public void Save(string targetPath)
{
try
{
// if the file isn't there, we can't deserialize.
if (String.IsNullOrEmpty(targetPath))
targetPath = GetDefaultPath();
SharpSerializerXmlSettings settings = new SharpSerializerXmlSettings();
SharpSerializer serializer = new SharpSerializer(settings);
// create a serialized representation of our MySettings instance, and write it to a file.
serializer.Serialize(this, targetPath);
}
catch (Exception err)
{
throw new InvalidOperationException(string.Format("Error in MySettings.Save(string targetPath):\r\nMessage:\r\n{0}\r\nStackTrace:\r\n{1}", err.Message, err.StackTrace), err);
}
}
public static string GetDefaultPath()
{
string result = string.Empty;
try
{
// Use Reflection to get the path of the Assembly MySettings is defined in.
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
// remove the file:// prefix for local files, or file:/// for network/unc paths
if (path.StartsWith("file:///"))
path = path.Remove(0, "file:///".Length);
else if (path.StartsWith("file://"))
path = path.Remove(0, "file://".Length);
// get the path without filename of the assembly
path = System.IO.Path.GetDirectoryName(path);
// append default filename "MySettings.xml" as default filename for the settings.
return System.IO.Path.Combine(path, "MySettings.xml");
}
catch (Exception err)
{
throw new InvalidOperationException(string.Format("Error in MySettings.GetDefaultPath():\r\nMessage:\r\n{0}\r\nStackTrace:\r\n{1}", err.Message, err.StackTrace), err);
}
}
}
public class Test
{
public void Test()
{
// create settings for testing
MySettings settingsTest = new MySettings();
// save settings to file. You could also pass a path created from a SaveFileDialog, or sth. similar.
settingsTest.Save(MySettings.GetDefaultPath());
// Load settings. You could also pass a path created from an OpenFileDialog.
MySettings anotherTest = MySettings.Load(MySettings.GetDefaultPath());
// do stuff with the settings.
}
}