1

私はstackoverflowを初めて使用しますが、試してみるべきだと思いました...

だから私がやろうとしているのは、他のプログラムがアクセスできるファイルに変数を保存することです...たとえば、すべてのセットアップデータ(データベース情報、文字列、数値など)を処理するセットアップアプリケーションがあります、またはブール値)。私が考えたのは、それらをプロパティファイルやテキストファイルなどのファイルに保存し、別のプログラムがそれらを読み取ってその設定ファイルを変更できるようにすることでした。誰かが私を適切な方向に向けてくれませんか?

ありがとうwaco001

4

5 に答える 5

3

C# を使用している場合は、すべての設定を別のクラスに入れてから、XmlSerialization を使用して保存することをお勧めします。そうすれば、最小限のコードで機能する機能が得られ、データを形式で保存できます。他のアプリケーションで読みやすい。

それを行う方法のサンプルが複数あります。例: http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm

于 2012-12-30T22:50:05.913 に答える
1

設定クラスを作成し、それをシリアル化/逆シリアル化します。また、構成を別のオブジェクトにカプセル化すると、プロパティ グリッドを使用して管理できるという利点が追加されます。通常、構成ファイルを使用してこれを行います。これは小さな例です。

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Generate.Test
{
    /// <summary>
    /// The configuration class.
    /// </summary>
    [Serializable, XmlRoot("generate-configuration")]
    public class Configuration : ISerializable
    {
        #region Fields

        private string inputPath  = string.Empty;
        private string outputPath = string.Empty;
        private int    maxCount   = 0;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Configuration" /> class.
        /// </summary>
        public Configuration()
        {
        }

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the output path.
        /// </summary>
        /// <value>
        /// The output path.
        /// </value>
        [XmlElement("output-path")]
        public string OutputPath
        {
            get { return this.outputPath; }
            set { this.outputPath = value; }
        }

        /// <summary>
        /// Gets or sets the input path.
        /// </summary>
        /// <value>
        /// The input path.
        /// </value>
        [XmlElement("input-path")]
        public string InputPath
        {
            get { return this.inputPath; }
            set { this.inputPath = value; }
        }

        /// <summary>
        /// Gets or sets the max count.
        /// </summary>
        /// <value>
        /// The max count.
        /// </value>
        [XmlElement("max-count")]
        public int MaxCount
        {
            get { return this.maxCount; }
            set { this.maxCount = value; }
        }
        #endregion

        #region ISerializable Members

        /// <summary>
        /// Gets the object data.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        /// <exception cref="System.ArgumentNullException">thrown when the info parameter is empty.</exception>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");

            info.AddValue("output-path", this.OutputPath);
            info.AddValue("input-path", this.InputPath);
            info.AddValue("max-count", this.MaxCount);
        }

        #endregion
    }
}

したがって、逆シリアル化するには (_configurationPath は、構成が格納されている xml のパスです):

        if (File.Exists(_configurationPath))
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
                Stream        stream     = new FileStream(_configurationPath, FileMode.Open, FileAccess.Read);
                Configuration config     = (Configuration)serializer.Deserialize(stream);

                _inputPath  = config.InputPath;
                _outputPath = config.OutputPath;
                _maxCount   = config.MaxCount;
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error cargando el archivo de configuración '{0}':\n{1}", _configurationPath, exception);
            }
        } 

そしてシリアル化するには:

    Configuration configuration = new Configuration(); // Create the object

    // Set the values

    configuration.InputPath  = @".\input";
    configuration.OutputPath = @".\output";
    configuration.MaxCount   = 1000;

    // Serialize

    XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
    Stream stream = new FileStream(_configurationPath, FileMode.Open, FileAccess.Write);
    serializer.Serialize(stream, configuration);

それが役に立てば幸い。

于 2012-12-30T23:12:38.940 に答える
1

Visual Studio プロジェクトでサポートされている App.config を使用してみてください。

于 2012-12-30T22:47:55.777 に答える
0

典型的な方法は、 を作成しXmlDocument、それに適切な名前のノードとアトリビュートを入力し、 を介して書き出すことSave()です。これには、他のほとんどの環境で XML を読み取って解析できるという利点があります。

もう 1 つの「共通語」は JSON です。これは JSON を介して簡単に記述でき、JSON.NET他のほとんどの環境で「理解」されます。

于 2012-12-30T22:49:14.653 に答える
0

アプリケーション間でデータを共有することだけが必要な場合は、WCF を検討する必要があります。

または、既存の .NET API for XML を使用して、データの作成と解析の両方を行うことができます。次に、システム IO を使用してハード ドライブに保存します。

于 2012-12-30T22:51:00.373 に答える