[英]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.