我在哪里可以存储这些设置? [英] Where can I store these settings?

查看:64
本文介绍了我在哪里可以存储这些设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我正在开发一个需要存储一些特定于机器的

设置的应用程序。该应用程序将在网络上以
的顺序发布,以使客户保持最新版本。

因此,我无法将这些设置存储在App.Config

文件,因为每次应用程序都会更新,并且

似乎不是防止这种情况的方法。


我的大多数应用程序设置都保存在数据库中,但是在建立数据库连接之前我需要一些




我需要的是一种存储这些的方法,以便每台机器可以更改它们,并且最好用文本编辑器进行编辑。

(我不想把它们放在注册表中,因为我希望它很简单

将设置从一台机器复制到另一台机器上。


在.Net中是否有对INI文件的支持?

有更好的解决方案吗?

谢谢。


Paul Cheetham

解决方案

2006年5月24日星期三11:02:54 +0100,Paul Cheetham

< PA ****** @ dsl.pipex .COM>写道:




我正在开发一个需要存储一些特定于机器的设置的应用程序。该应用程序将在网络上发布,以便使客户端保持最新版本。
因此,我无法将这些设置存储在App.Config
文件中,因为每次应用程序都会更新,并且似乎没有一种方法可以阻止这种情况。

我的大多数应用程序设置都保存在数据库中,但是在建立数据库连接之前有一些我需要的东西。

我需要的是一种存储这些的方法,以便可以在每台机器上更改它们,以及最好用文本编辑器编辑。
(我不想把它们放在注册表中,因为我希望它很容易将设置从一台机器复制到另一台机器上) />
.Net中有对INI文件的支持吗?
有更好的解决方案吗?

谢谢。

Paul Cheetham



也许你可以写一个带有所需属性的Settings类,并且

将其序列化/反序列化为xml文件(XmlSerializer)?


-

Ludwig Stuyck
http://www.coders-lab.be

从Delphi背景来到C#/ .Net我认为存储本地选项的简单性有点缺乏所以我已经创建了一个类来实现

只是你想要的东西。


1.你需要拿下面的代码并创建Settings.cs


2.然后在你的程序中创建一个来自SettingsObjectBase

的新类,它具有你需要存储在其中的所有设置。你甚至可以收集

等等。


公共类MyAppSettings:SettingsObjectBase

{

public string name;

public string Address;

public List< MyStuff> MyList = new List< MyStuff>();

}


3.然后在程序开始时执行以下操作:

SettingsStore< MyAppSettings> MySettingsStore = new

SettingsStore< MyAppSettings>(SettingsStoreLocation .UserApplicationLevel)


4.一旦你声明它就会自动加载(如果可以的话)。但在此之后

你可以做SettingsStore.Load()和SettingsStore.Save()


5.然后你需要做的就是在整个过程中使用MyAppSettings你的

程序来访问你的设置。


如果这个课程对任何人都有用,请告诉我。

设置代码。 cs:


使用System;

使用System.Collections.Generic;

使用System.Text;

使用System.Xml.Serialization;

使用System.IO;

使用System.Windows.Forms;


namespace YourCompanyName.General

{

///< summary>

///放置设置商店的位置

///< / summary>

public enum SettingsStoreLocation

{

///< summary>

///在指定的文件中

///< / summary>

SpecifiedFile,

///<摘要>

///在产品文件夹的用户主目录中

/// < / summary>

UserProductLevel,

///< summary>

///在产品的用户主目录中/ appname文件夹

///< / summary>

UserApplicationLevel,

///< summary>

///在产品文件夹的公共主目录中

///< / summary>

AllUsersProductLevel,

/// < summary>

///在product / appname文件夹的公共主目录中

///< / summary>

AllUsersApplicationLevel

}


///< summary>

///用于在XML中存储设置的通用类

///< / summary>

公共类SettingsStore< T>其中T:SettingsObjectBase,new()

{

#region Construction

///< summary>

///标准存储位置的构造函数+零件文件名

///< / summary>

///< param name =" Location"> ;存储设置的位置< / param>

///< param name =" FileName">文件名的最后一部分,不包括



///路径除非使用SpecifiedFile,否则这是

///完整文件名< / param>

public SettingsStore( SettingsStoreLocation位置,字符串

FileName)

{

SetupLocation(Location,FileName);

}


///< summary>

///标准存储位置的构造函数

///< / summary>

///< param name =" Location">存储设置的位置< / param>

public SettingsStore(SettingsStoreLocation Location)

{

SetupLocation(Location,string.Empty);

}


///< ;摘要>

///具有完整文件名的指定存储位置的构造函数

///< / summary>

/// < param name =" FileName">存储

///设置的完整文件名< / param>

public SettingsStore(string FileName)

{

SetupLocation(SettingsStoreLocation.SpecifiedFile,FileName);

}


///< ;摘要>

///您不知道的指定文件位置的构造函数

尚未。

///您必须设置FileName属性在调用Save / Load之前。

///< / summary>

///< param name =" Location">< / param> ;

public SettingsStore()

{

SetupLocation(SettingsStoreLocation.SpecifiedFile,

string.Empty); < br $>
}


//设置fi的位置le将被存储

void SetupLocation(SettingsStoreLocation Location,string FileName)

{

_Location = Location;


if(_Location!= SettingsStoreLocation.SpecifiedFile)

if(FileName.Trim()== string.Empty)

FileName =" Settings.xml" ;


开关(_Location)

{

case SettingsStoreLocation.SpecifiedFile:

_FileName = FileName ;

休息;

case SettingsStoreLocation.UserProductLevel:

_FileName = Environment.GetFolderPath(

Environment.SpecialFolder。 ApplicationData)+

Path.DirectorySeparatorChar +

Application.CompanyName +

Path.DirectorySeparatorChar +

_ProductName + Path .DirectorySeparatorChar +

FileName;

break;

case SettingsStoreLocation.UserApplicationLevel:

_FileName = Environment.GetFolderPath(

Environment.SpecialFolder.ApplicationD ata)+

Path.DirectorySeparatorChar +

Application.CompanyName +

Path.DirectorySeparatorChar +

_ProductName + Path .DirectorySeparatorChar +

Path.GetFileNameWithoutExtension(Application.Execu tablePath)

+

Path.DirectorySeparatorChar +

FileName ;

休息;

case SettingsStoreLocation.AllUsersProductLevel:

_FileName = Environment.GetFolderPath(

Environment.SpecialFolder。 CommonApplicationData)+

Path.DirectorySeparatorChar +

Application.CompanyName +

Path.DirectorySeparatorChar +

_ProductName + Path .DirectorySeparatorChar +

FileName;

break;

case SettingsStoreLocation.AllUsersApplicationLevel:

_FileName = Environment.GetFolderPath(

Environment.SpecialFolder.CommonApplicationData)+

Path.DirectorySeparatorChar +

Application.CompanyName +

Path.DirectorySeparatorChar +

_ProductName + Path.DirectorySeparatorChar +

Path.GetFileNameWithoutExtension(Application.Execu tablePath)

+

Path.DirectorySeparatorChar +

FileName;

break;

}


if(( _FileName!= string.Empty)&& (File.Exists(_FileName)))

加载();

else

重置();

}

#endregion


#region设置访问

//设置对象的内部存储

T _Settings;

///< summary>

///保存实际设置的设置对象

///< /摘要>

公共T设置

{

get

{

return _Settings ;

}

}

#endregion


#region存储

SettingsStoreLocation _Location;

///< summary>

///将存储设置的位置

///< / summary>

public SettingsStoreLocation Location

{

get

{

return _Location;

}

}


string _FileName;

///< summary>

///将存储设置的文件名

///< / summary>
公共字符串FileName

{

get

{

return _FileName;

}

设定

{

_FileName = value;

_Location = SettingsStoreLocation.SpecifiedFile ;

}

}


string _ProductName = Application.ProductName;

///< ;摘要>

///这默认为Application.ProductName但您可以更改它

///例如如果你想创建一个共享的产品名称来支持

设置

///在一组产品之间共享

///< / summary>

公共字符串ProductName

{

get

{

return _ProductName;

}

set

{

_ProductName = value;

}

}


///< summary>

///将设置保存到磁盘

///< / summary>

public void保存()

{

if(FileName.Trim()== string .Empty)

抛出新的异常(没有指定FileName);


Directory.CreateDirectory(Path.GetDirectoryName(_F ileName));


使用(StreamWriter Writer = new StreamWriter(_FileName))

{

XmlSerializer Serializer = new XmlSerializer(typeof(T) );


Serializer.Serialize(Writer,_Settings);

}


}


///< su mmary>

///从磁盘加载设置。如果您在创建对象时指定了一个位置,则会自动发生这种情况。

///< / summary>

public void加载()

{

if(FileName.Trim()== string.Empty)

抛出新的异常(No FileName has已指定);


if(!File.Exists(_FileName))

抛出新异常(文件不存在);


使用(FileStream Reader = new FileStream(_FileName,

FileMode.Open))

{

XmlSerializer Serializer = new XmlSerializer(typeof(T));


_Settings =(T)Serializer.Deserialize(Reader);

}

}


///< summary>

///将设置重置为默认状态

///< / summary>

public void重置()

{

_Settings = new T();

}


///< summary>

///将设置重置为默认状态和delet es

存储文件

///< / summary>

public void删除()

{

if(File.Exists(_FileName))

File.Delete(_FileName);


重置();

}

#endregion

}


///< summary>

///设置对象的基类。

///您必须从此继承您的设置对象。

///< / summary>

公共抽象类SettingsObjectBase

{

}


}

- -

Andrew Cutforth - AJC软件 - www.ajcsoft.com

最好的文件夹同步和目录比较工具。

AJC Active Backup立即存档你编辑的每个文件给你

无限撤销和自动修改控制。永远不会丢失你的数据。




谢谢安德鲁。

这看起来像一个有用的类,虽然它'对于我在这里做什么来说有点矫枉过正,因为我只需要在程序启动时阅读3或4个设置。

我已经有了设置类从

数据库表中提取信息。 (如果你想要这个让我知道,我可以发给你代码)

我想我会回到原来很好的ini档案。

保罗。


Andrew写道:

从Delphi背景来到C#/ .Net我觉得存储本地选项的难易程度有点缺乏我创建了一个类,它只是你想要的类型。

1.你需要使用下面的代码并创建Settings.cs

2然后在你的程序中创建一个来自SettingsObjectBase
的新类,它具有你需要存储在其中的所有设置。你甚至可以拥有
集合等。

公共类MyAppSettings:SettingsObjectBase
{
公共字符串名称;
公共字符串地址;
公开列表与LT我的资料GT; MyList = new List< MyStuff>();
}
3.然后在程序开始时执行以下操作:
SettingsStore< MyAppSettings> MySettingsStore = new
SettingsStore< MyAppSettings>(SettingsStoreLocation .UserApplicationLevel)

4.一旦你声明它就会自动加载(如果可以的话)。但在此之后
你可以做SettingsStore.Load()和SettingsStore.Save()

5.然后你需要做的就是在你的整个
程序中使用MyAppSettings来访问你的设置。

请告诉我这个课对任何人都有用。

设置代码:

使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.Xml.Serialization;
使用System.IO;
使用System.Windows.Forms; {
///< summary>
///放置设置商店的位置
///< / summary> ;
公共枚举SettingsStoreLocation
//
< summary>
///在指定的文件中
///< / summary>
SpecifiedFile,
///< summary>
///在产品文件夹的用户主目录中
///< / summary>
UserProductLeve l,
///< summary>
///在product / appname文件夹的用户主目录中
///< / summary>
UserApplicationLevel,
///< summary>
///在产品文件夹的公共主目录中
///< / summary>
AllUsersProductLevel,
// /< summary>
///在product / appname文件夹的公共主目录中
///< / summary>
AllUsersApplicationLevel
}

///< summary>
///用于以XML格式存储设置的通用类
///< / summary>
公共类SettingsStore< T>其中T:SettingsObjectBase,new()
{
#region Construction
///< summary>
///标准存储位置的构造函数+零件文件名 ///< / summary>
///< param name =" Location">存储设置的位置< / param>
///< param name =" ; FileName">文件名的最后一部分除了
///路径,除非使用SpecifiedFile,在这种情况下这是
///完整文件名< / param>
public SettingsStore(SettingsStoreLocation位置,字符串
FileName)
{
SetupLocation(Location,FileName);
}

///<摘要>
///标准存储位置的构造函数
///< / summary>
///< param name =" Location">存储设置的位置< ; / param>
公共集tingsStore(SettingsStoreLocation Location)
{
SetupLocation(Location,string.Empty);
}
///< summary>
///具有完整文件名的指定存储位置的构造函数
///< / summary>
///< param name =" FileName">存储
///设置< / param>
公共SettingsStore(字符串FileName)
{
SetupLocation(SettingsStoreLocation.SpecifiedFile,FileName);
}

///< summary>
///您不知道的指定文件位置的构造函数
///您必须在调用Save / Load之前设置FileName属性。
///< / summary>
///< param name =" Location">< / param>
公共设置商店()
{
SetupLocation(设置sStoreLocation.SpecifiedFile,
string.Empty);
}
//安装文件的位置
void SetupLocation(SettingsStoreLocation Location,string FileName)
{
_Location = Location;

if(_Location!= SettingsStoreLocation.SpecifiedFile)
if(FileName.Trim()== string.Empty)
FileName =" Settings.xml";

switch(_Location)
{
案例SettingsStoreLocation.SpecifiedFile:
_FileName = FileName;
break;
case SettingsStoreLocation.UserProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData)+
Path.DirectorySeparatorChar +
Application.CompanyName + Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.UserApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData)+
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension (Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersProductLevel:
_FileName = Environment.GetFolderPath( Environment.SpecialFolder.CommonApplicationData)+
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData)+
Path.DirectorySeparatorChar +
应用程序。 CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
}
if((_FileName!= string.Empty)&& (File.Exists(_FileName)))
加载();

重置();
}
#endregion

#区域设置访问
//设置对象的内部存储
T _Settings;
///< summary>
///保存实际设置的设置对象
///< / summary>
公共T设置
{
获取
{
返回_设置;
}
} #endregion

#region存储
SettingsStoreLocation _Location;
///< summary>
///将存储设置的位置
/ //< / summary>
公共设置商店位置
{
获取
{
返回_位置;
}
}

string _FileName;
///< summary>
///将存储设置的文件名
///< / summary>
公共字符串FileName
{
获取
{
返回_文件名;
}
设置
{_FileName = value;
_Location = SettingsStoreLocation .SpecifiedFile;
}


string _ProductName = Application.ProductName;
///< summary>
///默认为Application .ProductName但你可以改变它
///例如如果你想创建一个共享产品名称来保持
///在一组产品之间共享
///< / summary>
公共字符串ProductName
{
获得
{
返回_ProductName;
}
设置
{
产品名称=值;
}
}

///< summary>
///将设置保存到磁盘
///< / summary>
public void Save( )
{
if(FileName.Trim()== string.Empty)
抛出新的异常(没有指定FileName);

目录.CreateDirectory(Path.GetDirectoryName(_F ileName));

使用(StreamWriter Writer = new StreamWriter(_FileName))
{xmlSerializer Serializer = new XmlSeriali zer(typeof(T));

Serializer.Serialize(Writer,_Settings);
}

}
/// < summary>
///从磁盘加载设置。如果您在创建对象时指定位置,则会自动发生这种情况。
///< / summary>
public void Load()
{
if(FileName.Trim()== string.Empty)
抛出新的异常(没有指定FileName);

if(!File.Exists(_FileName))
抛出新的异常(文件不存在);

使用(FileStream Reader = new FileStream(_FileName,
FileMode.Open))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));

_Settings =(T)Serializer.Deserialize(Reader);
}
}

///< summary>
///将设置重置为默认状态
///< / summary>
public void Reset()
{
_Settings = new T();
}

///< summary>
///将设置重置为默认状态并删除
存储文件
///< / summary> ;
public void删除()
{
if(File.Exists(_FileName))
File.Delete(_FileName);

重置() ;
}

}
///< summary>
///设置对象的基类。
/ //您必须从此继承您的设置对象。
///< / summary>
公共抽象类SettingsObjectBase
{
}

}




Hi,

I am developing an application that needs to store some machine-specific
settings. The application is going to be published on the network in
order to keep the clients on the latest version.
Because of this, I am unable to store these settings in the App.Config
file, as this gets updated every time the application does, and there
doesn''t appear to be a way of preventing this.

Most of my application settings are kept in the database, but there are
some that I need before the database connection is established.

What I need is a way of storing these so that they can be changed on
each machine, and that can preferably be edited with a text editor.
(I don''t want to put them in the registry, because I want it to be easy
to copy the settings from one machine to another)

Is there any support for INI files in .Net?
Is there a better solution?
Thanks.

Paul Cheetham

解决方案

On Wed, 24 May 2006 11:02:54 +0100, Paul Cheetham
<PA******@dsl.pipex.com> wrote:


Hi,

I am developing an application that needs to store some machine-specific
settings. The application is going to be published on the network in
order to keep the clients on the latest version.
Because of this, I am unable to store these settings in the App.Config
file, as this gets updated every time the application does, and there
doesn''t appear to be a way of preventing this.

Most of my application settings are kept in the database, but there are
some that I need before the database connection is established.

What I need is a way of storing these so that they can be changed on
each machine, and that can preferably be edited with a text editor.
(I don''t want to put them in the registry, because I want it to be easy
to copy the settings from one machine to another)

Is there any support for INI files in .Net?
Is there a better solution?
Thanks.

Paul Cheetham



Maybe you can write a Settings class with the required properties, and
serialize/deserialize it to an xml file (XmlSerializer)?

--
Ludwig Stuyck
http://www.coders-lab.be


Having come to C#/.Net from a Delphi background I thought the ease of
storing local options was a bit lacking so I have created a class that does
just the type of thing you want.

1. You need to take the code below and create Settings.cs

2. Then in your program create a new class descended from SettingsObjectBase
that has all the settings you need to store in it. You can even have
collections etc.

public class MyAppSettings : SettingsObjectBase
{
public string Name;
public string Address;
public List<MyStuff> MyList = new List<MyStuff>();
}

3. Then at the start of your program do this:
SettingsStore<MyAppSettings> MySettingsStore = new
SettingsStore<MyAppSettings>(SettingsStoreLocation .UserApplicationLevel)

4. It will auto load as soon as you decclare it (if it can). But after this
you can do SettingsStore.Load() and SettingsStore.Save()

5. Then all you need to do is work with MyAppSettings throughout your
program to access your settings.

Let me know if this class is useful to anyone.
Code for Settings.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace YourCompanyName.General
{
/// <summary>
/// Where to place the settings store
/// </summary>
public enum SettingsStoreLocation
{
/// <summary>
/// In the file specified
/// </summary>
SpecifiedFile,
/// <summary>
/// In the user home directory in the product folder
/// </summary>
UserProductLevel,
/// <summary>
/// In the user home directory in the product/appname folder
/// </summary>
UserApplicationLevel,
/// <summary>
/// In the common home directory in the product folder
/// </summary>
AllUsersProductLevel,
/// <summary>
/// In the common home directory in the product/appname folder
/// </summary>
AllUsersApplicationLevel
}

/// <summary>
/// Generic class for storing settings in XML
/// </summary>
public class SettingsStore<T> where T : SettingsObjectBase, new()
{
#region Construction
/// <summary>
/// Constructor for standard storage location + part file name
/// </summary>
/// <param name="Location">The place to store the settings</param>
/// <param name="FileName">The last part of the file name excluding
the
/// path unless using SpecifiedFile in which case this is the
/// full file name</param>
public SettingsStore(SettingsStoreLocation Location, string
FileName)
{
SetupLocation(Location, FileName);
}

/// <summary>
/// Constructor for standard storage location
/// </summary>
/// <param name="Location">The place to store the settings</param>
public SettingsStore(SettingsStoreLocation Location)
{
SetupLocation(Location, string.Empty);
}

/// <summary>
/// Constructor for specified storage location with full file name
/// </summary>
/// <param name="FileName">The full filename for where to store the
/// settings</param>
public SettingsStore(string FileName)
{
SetupLocation(SettingsStoreLocation.SpecifiedFile, FileName);
}

/// <summary>
/// Constructor for a specified file location that you do not know
yet.
/// You must set the FileName propery before calling Save/Load.
/// </summary>
/// <param name="Location"></param>
public SettingsStore()
{
SetupLocation(SettingsStoreLocation.SpecifiedFile,
string.Empty);
}

// Setup where the file will be stored
void SetupLocation(SettingsStoreLocation Location, string FileName)
{
_Location = Location;

if (_Location != SettingsStoreLocation.SpecifiedFile)
if (FileName.Trim() == string.Empty)
FileName = "Settings.xml";

switch (_Location)
{
case SettingsStoreLocation.SpecifiedFile:
_FileName = FileName;
break;
case SettingsStoreLocation.UserProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.UserApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
}

if ((_FileName != string.Empty) && (File.Exists(_FileName)))
Load();
else
Reset();
}
#endregion

#region Settings Access
// Internal store of settings object
T _Settings;
/// <summary>
/// The settings object holding the actual settings
/// </summary>
public T Settings
{
get
{
return _Settings;
}
}
#endregion

#region Storage
SettingsStoreLocation _Location;
/// <summary>
/// Where the settings will be stored
/// </summary>
public SettingsStoreLocation Location
{
get
{
return _Location;
}
}

string _FileName;
/// <summary>
/// The filename where the settings will be stored
/// </summary>
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
_Location = SettingsStoreLocation.SpecifiedFile;
}
}

string _ProductName = Application.ProductName;
/// <summary>
/// This defaults to Application.ProductName but you can change it
/// e.g. if you want to create a shared product name to hold
settings
/// shared between a group of products
/// </summary>
public string ProductName
{
get
{
return _ProductName;
}
set
{
_ProductName = value;
}
}

/// <summary>
/// Save the settings to disk
/// </summary>
public void Save()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");

Directory.CreateDirectory(Path.GetDirectoryName(_F ileName));

using (StreamWriter Writer = new StreamWriter(_FileName))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));

Serializer.Serialize(Writer, _Settings);
}

}

/// <summary>
/// Load the settings from disk. This happens automatically if you
/// specify a location when creating the object.
/// </summary>
public void Load()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");

if (!File.Exists(_FileName))
throw new Exception("The file does not exist");

using (FileStream Reader = new FileStream(_FileName,
FileMode.Open))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));

_Settings = (T)Serializer.Deserialize(Reader);
}
}

/// <summary>
/// Reset the settings to their default state
/// </summary>
public void Reset()
{
_Settings = new T();
}

/// <summary>
/// Resets the settings to their default state and deletes the
storage file
/// </summary>
public void Delete()
{
if (File.Exists(_FileName))
File.Delete(_FileName);

Reset();
}
#endregion
}

/// <summary>
/// Base class for settings objects.
/// You must inherit your settings object from this.
/// </summary>
public abstract class SettingsObjectBase
{
}

}
--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.



Thanks Andrew.
That looks like a useful class, although it''s a bit of overkill for what
I''m doing here, as I only need to read 3 or 4 settings at program startup.
I already have a settings class that pulls its information from a
database table. (If you want this let me know and I can send you the code)
I think I''ll fall back to the good old ini files.
Paul.

Andrew wrote:

Having come to C#/.Net from a Delphi background I thought the ease of
storing local options was a bit lacking so I have created a class that does
just the type of thing you want.

1. You need to take the code below and create Settings.cs

2. Then in your program create a new class descended from SettingsObjectBase
that has all the settings you need to store in it. You can even have
collections etc.

public class MyAppSettings : SettingsObjectBase
{
public string Name;
public string Address;
public List<MyStuff> MyList = new List<MyStuff>();
}

3. Then at the start of your program do this:
SettingsStore<MyAppSettings> MySettingsStore = new
SettingsStore<MyAppSettings>(SettingsStoreLocation .UserApplicationLevel)

4. It will auto load as soon as you decclare it (if it can). But after this
you can do SettingsStore.Load() and SettingsStore.Save()

5. Then all you need to do is work with MyAppSettings throughout your
program to access your settings.

Let me know if this class is useful to anyone.
Code for Settings.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace YourCompanyName.General
{
/// <summary>
/// Where to place the settings store
/// </summary>
public enum SettingsStoreLocation
{
/// <summary>
/// In the file specified
/// </summary>
SpecifiedFile,
/// <summary>
/// In the user home directory in the product folder
/// </summary>
UserProductLevel,
/// <summary>
/// In the user home directory in the product/appname folder
/// </summary>
UserApplicationLevel,
/// <summary>
/// In the common home directory in the product folder
/// </summary>
AllUsersProductLevel,
/// <summary>
/// In the common home directory in the product/appname folder
/// </summary>
AllUsersApplicationLevel
}

/// <summary>
/// Generic class for storing settings in XML
/// </summary>
public class SettingsStore<T> where T : SettingsObjectBase, new()
{
#region Construction
/// <summary>
/// Constructor for standard storage location + part file name
/// </summary>
/// <param name="Location">The place to store the settings</param>
/// <param name="FileName">The last part of the file name excluding
the
/// path unless using SpecifiedFile in which case this is the
/// full file name</param>
public SettingsStore(SettingsStoreLocation Location, string
FileName)
{
SetupLocation(Location, FileName);
}

/// <summary>
/// Constructor for standard storage location
/// </summary>
/// <param name="Location">The place to store the settings</param>
public SettingsStore(SettingsStoreLocation Location)
{
SetupLocation(Location, string.Empty);
}

/// <summary>
/// Constructor for specified storage location with full file name
/// </summary>
/// <param name="FileName">The full filename for where to store the
/// settings</param>
public SettingsStore(string FileName)
{
SetupLocation(SettingsStoreLocation.SpecifiedFile, FileName);
}

/// <summary>
/// Constructor for a specified file location that you do not know
yet.
/// You must set the FileName propery before calling Save/Load.
/// </summary>
/// <param name="Location"></param>
public SettingsStore()
{
SetupLocation(SettingsStoreLocation.SpecifiedFile,
string.Empty);
}

// Setup where the file will be stored
void SetupLocation(SettingsStoreLocation Location, string FileName)
{
_Location = Location;

if (_Location != SettingsStoreLocation.SpecifiedFile)
if (FileName.Trim() == string.Empty)
FileName = "Settings.xml";

switch (_Location)
{
case SettingsStoreLocation.SpecifiedFile:
_FileName = FileName;
break;
case SettingsStoreLocation.UserProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.UserApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
}

if ((_FileName != string.Empty) && (File.Exists(_FileName)))
Load();
else
Reset();
}
#endregion

#region Settings Access
// Internal store of settings object
T _Settings;
/// <summary>
/// The settings object holding the actual settings
/// </summary>
public T Settings
{
get
{
return _Settings;
}
}
#endregion

#region Storage
SettingsStoreLocation _Location;
/// <summary>
/// Where the settings will be stored
/// </summary>
public SettingsStoreLocation Location
{
get
{
return _Location;
}
}

string _FileName;
/// <summary>
/// The filename where the settings will be stored
/// </summary>
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
_Location = SettingsStoreLocation.SpecifiedFile;
}
}

string _ProductName = Application.ProductName;
/// <summary>
/// This defaults to Application.ProductName but you can change it
/// e.g. if you want to create a shared product name to hold
settings
/// shared between a group of products
/// </summary>
public string ProductName
{
get
{
return _ProductName;
}
set
{
_ProductName = value;
}
}

/// <summary>
/// Save the settings to disk
/// </summary>
public void Save()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");

Directory.CreateDirectory(Path.GetDirectoryName(_F ileName));

using (StreamWriter Writer = new StreamWriter(_FileName))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));

Serializer.Serialize(Writer, _Settings);
}

}

/// <summary>
/// Load the settings from disk. This happens automatically if you
/// specify a location when creating the object.
/// </summary>
public void Load()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");

if (!File.Exists(_FileName))
throw new Exception("The file does not exist");

using (FileStream Reader = new FileStream(_FileName,
FileMode.Open))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));

_Settings = (T)Serializer.Deserialize(Reader);
}
}

/// <summary>
/// Reset the settings to their default state
/// </summary>
public void Reset()
{
_Settings = new T();
}

/// <summary>
/// Resets the settings to their default state and deletes the
storage file
/// </summary>
public void Delete()
{
if (File.Exists(_FileName))
File.Delete(_FileName);

Reset();
}
#endregion
}

/// <summary>
/// Base class for settings objects.
/// You must inherit your settings object from this.
/// </summary>
public abstract class SettingsObjectBase
{
}

}



这篇关于我在哪里可以存储这些设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆