在用户设置中存储数组[,]

时间:2012-07-31 11:01:31

标签: c# arrays multidimensional-array settings

我的问题非常简单。如何在程序的设置中存储双数组,并将其从中删除。

我已经知道了:

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();

我做得对吗?现在阵列还在用户设置中吗?

我怎样才能解决它(具有正确用户ID的设置)?

我知道这可能听起来很疯狂,但我认为这是最好的方法。但如果你们知道更好的方法,请告诉我。我

修改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; }

而且我认为这会从阵列中获取用户设备,但不知何故他们没有。

3 个答案:

答案 0 :(得分:3)

使用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。

答案 1 :(得分:2)

好吧,通常我只使用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.
       }
}

答案 2 :(得分:1)

啊,我看到了问题。 正如您在XML文件中看到的那样,MySettings实例(settingsTest)中的数组为null。 那是因为你在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());