在Appconfig文件中如何编写代码 [英] In Appconfig file how to write the code

查看:88
本文介绍了在Appconfig文件中如何编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c Sharp设计应用程序.
因为我有以下三列.
租金,柴油服务,服务税.


租金231270
柴油发动机19767
Ser Tax 30124(231270 + 19767 * 12/100).
代码如下;

double o = Convert.ToDouble(txt_rentalcost.Text);
double p = Convert.ToDouble(txt_dieselservice.Text);
txt_servicetax.Text = Math.Round((o + p)* 0.12).ToString();

我当时正在计算服务税,服务税是12%.假设明年服务税的百分比是12.36.

我发布了上述问题,您说我们可以在App.config中编写代码.

在App.config中如何编写代码.请帮助我.第一次使用App.config时请帮助我.


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

i design application using c sharp.
in that i have threecolumn as follows.
Rental cost ,Diesel service, Service Tax.


Rental Cost 231270
Diesel ser 19767
Ser Tax 30124(231270 + 19767 * 12/100).
Code as follows;

double o = Convert.ToDouble(txt_rentalcost.Text);
double p = Convert.ToDouble(txt_dieselservice.Text);
txt_servicetax.Text = Math.Round((o + p) * 0.12).ToString();

i am doing service tax calculation at that time service tax is 12 percentage.suppose in next year service tax percentage is 12.36.

i post the above question you said that we can write the code in App.config.

In App.config how to write the code.please help me.first time i am using App.config help me.


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

推荐答案

在app.config文件中添加密钥
Adding Keys in app.config file
<appSettings>
    <add key="author" value="Palanisamy Veerasingam"/>
    <add key="article" value="Configuration Sections"/>
</appSettings>



在您的应用程序中获取键值



Retreiving the Key value in your application

return (ConfigurationSettings.AppSettings["author"]);




查看本文以获取更多信息
了解节处理程序-App.config文件 [




Check this article for more information
Understanding Section Handlers - App.config File[^]


使用设置文件:

在Visual Studio中,转到项目属性"-> 设置"

在显示的数据网格中,添加一个行,其中包含名称"-> TaxPercentage,Type = double,Scope = Application,Value = 12
这将在您的app.config中产生一个新部分:

Use a settings file:

In Visual Studio, go to "project properties" -> "Settings"

In the shown datagrid add a row with "Name"->TaxPercentage, Type=double, Scope=Application, Value=12
this will result in a new section in your app.config:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>



和存储的设置:



and the stored setting:

<applicationSettings>
        <WindowsFormsApplication1.Properties.Settings>
            <setting name="Setting" serializeAs="String">
                <value>0</value>
            </setting>
        </WindowsFormsApplication1.Properties.Settings>
    </applicationSettings>



要在Code中访问您的设置,只需添加一个using指令,例如:



to access your settings in Code just add a using directive like:

using WindowsFormsApplication1.Properties;



并使用以下命令访问您的可配置值:



and access your configurable value with:

double value = Settings.Default.TaxPercentage;



这将在您的应用程序中为您提供重量轻但类型安全的可配置值
-重量轻,因为您不必编写自定义配置部分,如果我必须管理我的应用程序/服务/等的许多可配置参数,我更喜欢这么做.
-类型安全,因为配置系统将确保您的设置始终为期望的类型

请参阅
此链接 [



this will give you a leightweight but typesafe configurable value in your application
- leightweight, cause you don´t have to write custom config sections, what i prefer if i have to manage many configurable parameters of my application/service/etc.
- typesafe, cause the configuration-system will make sure that the your setting will always be of the expected type

refer this link [^]for detailed information


这篇关于在Appconfig文件中如何编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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