C# 在 xml 中创建您自己的设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/396139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create your own settings in xml
提问by balexandre
I'm in a ASP.NET project where I need to give several parameters to the administrator that is going to install the website, like:
我在一个 ASP.NET 项目中,我需要向将要安装网站的管理员提供几个参数,例如:
AllowUserToChangePanelLayout
AllowUserToDeleteCompany
etc...
等等...
My question is, will be a good thing to add this into the web.config file, using my own configSession or add as a profile varibles? or should I create a XML file for this?
我的问题是,将它添加到 web.config 文件中,使用我自己的 configSession 或添加为配置文件变量是一件好事吗?或者我应该为此创建一个 XML 文件?
What do you do and what are the cons and favs?
你做什么,有什么缺点和喜好?
I originally thought about web.config but I then realized that I should mess up with Website configurations and my own web app configuration and that I should create a different file, them I read this postand now I'm on this place... should I do this or that?
我最初想到的是 web.config,但后来我意识到我应该弄乱网站配置和我自己的 Web 应用程序配置,我应该创建一个不同的文件,我阅读了这篇文章,现在我在这个地方......我应该这样做还是那样?
采纳答案by configurator
I usually use Settings - available via the project properties - Settings. These can be edited and saved in code, and I write a form / web page to edit them.
我通常使用设置 - 可通过项目属性 - 设置。这些可以编辑和保存在代码中,我写了一个表单/网页来编辑它们。
If you want to use the XML configuration, there's an attribute called file that reads external files. You could have a web.config file and a someothername.config file. The someothername.config would have settings like:
如果您想使用 XML 配置,有一个名为 file 的属性可以读取外部文件。您可以有一个 web.config 文件和一个 someothername.config 文件。someothername.config 将具有如下设置:
<appSettings>
<add key="ConnString" value="my conn string" />
<add key="MaxUsers" value="50" />
</appSettings>
And the web.config would have
而 web.config 会有
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="ExternalWeb.config">
<add key="MyKey" value="MyValue" />
</appSettings>
</configuration>
See DevXfor the example I stole.
有关我窃取的示例,请参阅DevX。
回答by balexandre
just to let you guys know that I did what configuratorrecommended but with a twist.
只是为了让你们知道我做了配置器推荐的操作,但有所不同。
instead of asking all the time (that I need) for
而不是一直要求(我需要)
System.Configuration.ConfigurationManager.AppSettings["myKey"];
I just created a static class that would pull this values with what we call by Strongly typed values (so you don't need to remember all the values)
我刚刚创建了一个静态类,它将使用我们称为强类型值的内容来提取这些值(因此您不需要记住所有值)
the mySettingsclass
该mySettings类
public static class mySettings
{
public enum SettingsType
{ UserPermitions, WebService, Alerts }
public enum SectionType
{ AllowChangeLayout, AllowUserDelete, MaximumReturnsFromSearch, MaximumOnBatch, SendTo }
public static String GetSettings(SettingsType type, SectionType section)
{
return
ConfigurationManager.AppSettings[
String.Format("{0}_{1}",
Enum.Parse(typeof(SettingsType), type.ToString()).ToString(),
Enum.Parse(typeof(SectionType), section.ToString()).ToString())
];
}
}
the web.configappSettings part
在web.config中的appSettings部分
<configuration>
<appSettings file="myApp.config">
<add key="UserPermitions_AllowChangeLayout" value="" />
<add key="UserPermitions_AllowUserDelete" value="" />
<add key="WebService_MaximumReturnsFromSearch" value="" />
<add key="Alerts_SendTo" value="" />
<add key="Alerts_MaximumOnBatch" value="" />
</appSettings>
</configuration>
the entire myApp.configfile
整个myApp.config文件
<?xml version="1.0" encoding="utf-8" ?>
<!--
###
### This file serves the propose of a quick configuration.
### Administrator can either change this values directly or use the
### Settings tab in the application.
###
-->
<appSettings>
<!-- *** User Access Configuration *** -->
<!-- Allow user to change the panels layout {1: Yes} {0: No} -->
<add key="UserPermitions_AllowChangeLayout" value="1" />
<!-- Allow user to delete a company fro monitoring -->
<add key="UserPermitions_AllowUserDelete" value="1" />
<!-- *** Web Service configuration *** -->
<!-- Maximum responses from the search service -->
<add key="WebService_MaximumReturnsFromSearch" value="10" />
<!-- *** Allerts configuration *** -->
<!-- Send the alerts to the email writeen below -->
<add key="Alerts_SendTo" value="[email protected]" />
<!-- Send an alert when user import more than the number bellow -->
<add key="Alerts_MaximumOnBatch" value="10" />
</appSettings>
So, now I call like this:
所以,现在我这样称呼:
p.value = mySettings.GetSettings(
mySettings.SettingsType.WebService,
mySettings.SectionType.MaximumReturnsFromSearch);
Hope that helps someone with the same problem :)
希望能帮助有同样问题的人:)
回答by Eli Makumba
You may also put your configurations in a settings file. In your project, open Properties and go to Settings which looks like so
您也可以将配置放在设置文件中。在您的项目中,打开“属性”并转到“设置”, 如下所示
To access the values in your code, use Properties.Settings.YourSettingName;
要访问代码中的值,请使用 Properties.Settings.YourSettingName;
Use Properties.Settings.Default.Reload();
to refresh your settings during runtime
用于Properties.Settings.Default.Reload();
在运行时刷新您的设置