65.9K
CodeProject 正在变化。 阅读更多。
Home

自定义 C# .NET 设置类

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.53/5 (7投票s)

2008年6月27日

CPOL

2分钟阅读

viewsIcon

41869

downloadIcon

573

修改 app.config 和 web.config 时遇到问题?使用您自己的设置类。易于添加新键并保存到磁盘。

引言

这是我写的第一篇文章,请大家多多包涵。这篇文章介绍了一种我用来将应用程序设置保存到永久存储的方法,实践证明它既有用又易于维护和添加新的设置成员。

背景

这样做的背景是,我在为我的 Windows/Web/移动应用程序保存一些简单的设置时,遇到了修改 app.configweb.config 设置文件时遇到的问题。因此,我编写了自己的类来保存应用程序设置,并且我一直沿用它(即使我现在已经学会了修改 app.config)。

使用代码

使用这段代码应该很简单。所有需要的类都包含在 clsSettings.cs 文件中,您可以将其添加到您的项目中。接下来,您应该将类的命名空间更改为与您的默认命名空间匹配,这样使用起来会更方便。

关于 clsSettings 类

该类有两个方法:SaveLoad,它们将 clsSettings 对象保存到/从指定的路径加载。它还有私有成员和公共属性,您可以根据需要进行添加/更改。

clsSetting 类有两个成员,它们本身也是类。一个名为 clsApplication,另一个名为 Paths。调用 Save 方法时,这两个类及其成员都会被保存。

[Serializable]
public class clsSettings
{
    #region private Fields

    private clsApplication appRelated = new clsApplication();
    private Paths paths = new Paths();

    #endregion

    #region Public properties

    public Paths Paths
    {
        get
        {
            return paths;
        }
        set
        {
            paths = value;
        }
    }

    public clsApplication AppRelated
    {
        get
        {
            return appRelated;
        }
        set
        {
            appRelated = value;
        }
    }

    #endregion

    #region Methods: Save, Load

    /// <summary>
    /// Saves this settings object to desired location
    /// </summary>
    /// <param name="fileName"></param>
    public void Save(string fileName)
    {
        // Insert code to set properties and fields of the object.
        XmlSerializer mySerializer = new XmlSerializer(typeof(clsSettings));
        // To write to a file, create a StreamWriter object.
        StreamWriter myWriter = new StreamWriter(fileName);
        mySerializer.Serialize(myWriter, this);
        myWriter.Close();
    }

    /// <summary>
    /// Returns a clsSettings object, loaded from a specific location
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public clsSettings Load(string fileName)
    {
        // Constructs an instance of the XmlSerializer with the type
        // of object that is being deserialized.
        XmlSerializer mySerializer = new XmlSerializer(typeof(clsSettings));
        // To read the file, creates a FileStream.
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        // Calls the Deserialize method and casts to the object type.
        clsSettings pos = (clsSettings)mySerializer.Deserialize(myFileStream);
        myFileStream.Close();
        return pos;
    }

    #endregion
}

您可以向这些类中的任何一个添加成员,它们也会被保存/加载。

演示中 clsApplication 成员类的样子如下:

public class clsApplication
{
    #region private fields
    private string version = "";
    private string lsStrIDLeft = "";
    private string lsStrIDRight = "";
    #endregion

    #region public properties
    public string LsStrIDLeft
    {
        get { return lsStrIDLeft; }
        set { lsStrIDLeft = value; }
    }
    public string LsStrIDRight
    {
        get { return lsStrIDRight; }
        set { lsStrIDRight = value; }
    }
    public string Version
    {
        get { return version; }
        set { version = value; }
    }
    #endregion
}

添加新成员的方式是添加一个私有成员,例如:

private string version = "";

然后右键单击版本号,并

clsSettings

这将生成用于获取/设置私有成员值的公共属性。

encapsulation1.png

我使用这个 clsSetting 类的方式是,向 Program 类添加一个公共静态成员,这样我就可以在应用程序的任何其他类或窗体中访问该设置。此外,我还向 Program 类添加了一个公共静态方法,该方法返回保存/加载设置的路径,如下所示:

static class Program
{
    //declare and initialize the public static clsSettings object
    public static clsSettings sets = new clsSettings();
    
    /// <summary>
    /// Returns the settings file path
    /// </summary>
    /// <returns></returns>
    public static string GetSettingPath()
    {
        return Application.StartupPath + "settings.xml";
    }
.
.
.

并可以从任何地方访问它,例如,如下所示:

this.Text += " " + Program.sets.AppRelated.Version;

要加载设置,请调用 clsSettings 实例的 Load 方法;例如,如下所示:

Program.sets = Program.sets.Load(Program.GetSettingPath());

要保存设置,请调用 clsSettings 实例的 Save 方法,如下所示:

Program.sets.Save(Program.GetSettingPath());

关注点

您可以使用此类来保存应用程序设置,例如数据库路径、数据库地址、各种 IP 地址、列表框、网格、组合框的最后选择值、最后打开的窗体、Web 服务地址等。

© . All rights reserved.