1

プログラムの設定にdouble配列を格納し、後でそれを取得するにはどうすればよいですか?


コード

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

そして、それは配列からユーザー設定を取得すると思いましたが、どういうわけかそうではありません。

4

4 に答える 4

3

System.Collections.Specialized.StringCollectionXML文字列(「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を解析します。

于 2012-07-31T11:13:22.570 に答える
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.
       }
}
于 2012-07-31T11:12:41.953 に答える
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());
于 2012-07-31T15:25:58.360 に答える
1

ああ、私たちは2012年にとても若かったのですが、代わりにJSONシリアライザーを使用してアイテムのリスト(または配列)を保存します。MRU私の例では、 doubleの代わりに名前の付いたクラスを使用していますが、考え方は同じです。

設定に

 // Extract from ObservableCollection<MRU>.
 List<MRU> asList = MRUS.ToList<MRU>();
 Properties.Settings.Default.MRUS = JsonSerializer.Serialize(asList);
 Properties.Settings.Default.Save();

設定外

var mruText = Properties.Settings.Default.MRUS;
return string.IsNullOrWhiteSpace(mruText) ? new List<MRU>()
    : JsonSerializer.Deserialize<List<MRU>>(mruText);
于 2021-11-08T06:43:58.880 に答える