[英]Save Settings Or Data C#
我是stackoverflow的新手,但认为我应该尝试一下...
所以我想做的就是将变量保存在其他程序可以访问的文件中...例如,我有一个设置应用程序,负责处理所有设置数据(例如数据库信息,字符串,数字)或布尔值)。 我当时想将它们保存到“属性”文件或文本文件之类的文件中,以便另一个程序可以读取它们并修改该设置文件。 有人可以指出我的正确方向吗?
谢谢waco001
如果您使用的是C#,我建议您将所有设置放在一个单独的类中,然后使用XmlSerialization保存它,这样一来,您将可以使用最少的代码来实现功能,并且您可以将数据以格式保存易于其他应用读取。
有多种示例可供使用,例如: http : //www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
尝试使用Visual Studio项目中支持的App.config。
创建一个设置类并对其进行序列化/反序列化,同样,如果将配置封装在另一个对象中,则还具有使用属性网格来管理它的额外好处,我通常使用我的配置文件来做到这一点,这是一个小例子:
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);
希望能帮助到你。
典型的方法是创建一个XmlDocument
,用适当命名的节点和属性填充它,然后通过Save()
将其写出。 这样做的好处是,大多数其他环境都可以读取XML并进行解析。
另一个“通用语言”是JSON,它可以通过JSON.NET
轻松编写,并且在大多数其他环境中都可以“理解”。
如果只需要在应用程序之间共享数据,则应研究WCF。
或者,您可以使用现有的XML NET API来创建和解析数据。 然后使用系统IO将其存储到硬盘驱动器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.