我的问题很简单。 你怎么能存储在程序的设置一个双阵列,并得到它出来。
我已经得到了这一点:
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;
所以你可以看到,现在用的是用户的ID来存储信息一起我。 而且我保存它是这样的:
Properties.Settings.Default.user_credits = user_credits;
Properties.Settings.Default.Save();
我这样做对吗? 现在在用户设置阵列仍?
我怎样才能把它弄出来的它(用正确的用户名的设置)?
我知道这听起来很疯狂,但我认为这是最好的方式。 但是,如果你们知道一个更好的方式,告诉我。 一世
编辑1:
我有这段代码:
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());
和运行代码后,这是XML文件的样子:
<Complex name="Root" type="WeProgram_Mail.MySettings, WeProgram_Mail, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<Properties>
<Null name="user_credits" />
</Properties>
现在,我不明白为什么不保存的阵列。 因为我有这条线
public string[,] user_credits { get; set; }
而且我认为这会拿起从数组中usersettings,但不知何故,他们没有。
啊,我看这个问题。 当你在XML文件中看到,在MySettings实例数组(settingsTest)为空。 那是因为你补settingsTest对象之外的阵列和从来没有接触或初始化settingsTest.user_credits ...
尝试以下方法:
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());
使用System.Collections.Specialized.StringCollection
设置并添加XML字符串(包含你喜欢“USER_NAME”或“USER_EMAIL”附加属性),以每个字符串:
var collection = new StringCollection {"<user_name>aaaa<user_name><user_email>asdfasd@asdfasd</user_email>"};
Properties.Settings.Default.MySetting = collection;
Properties.Settings.Default.Save();
当你需要的属性解析XML。
好了,平时我只是用http://www.sharpserializer.com/en/index.html
这是幼稚使用方便,快捷,可序列化或多或少的任何内容,包括字典,等等好处是,你可以序列化到多个目标格式,如二进制文件。
编辑:实施例用于与SharpSerializer序列化。 有没有编译的代码,但应该罚款。 缺点:要存储的属性必须是公共...
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.
}
}