您的位置:首页 > 移动开发

使用<appSettings>元素定义配置参数

2012-05-22 11:25 549 查看
可以使用应用程序配置文件中的<appSettings>元素保存专门用于应用程序的配置值。例如:

假设Myapp应用程序有一个名为MyInt参数:

<?xml version="1.0" encoding="utf-8" ?>

<configuration

xmls=http://schemas.microsoft.com/.netConfiguration/v2.0>

<appSettings>

<add key="MyInt" value="1234"/>

</appSettings>

</configuration>

下面是MyApp应用程序的代码。在每次执行的时候,代码都会取出MyInt参数的值再乘以10,然后保存新值。

Myapp。cs

using System.Configuration;

class Program{

static void main()

{

Configuration appCfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLeverl.None);

AppSettingsSection appSettings=appCfg.AppSettings;

int myInt;

if(int.TryParse(appSettings.Settings["MyInt"].Value, out myInt))

{

System.Console.WriteLine(myInt);

myInt*=10;

appSettings.Settings["MyInt"].value=myInt.ToString();

appCfg.Save();

}

}

}

这个方法存在的缺点:

(1)。 参数值没有类型。我们需要显示地将一个字符串解析为一个整型变量,以获取MyInt的值

(2)。 由于参数的名字是一个字符串,因此无法被编译器验证。这样会降低开发效率,因为开发者无法从智能感知中获益。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐