什么是App.config中的C#.NET?如何使用它? [英] What is App.config in C#.NET? How to use it?

查看:203
本文介绍了什么是App.config中的C#.NET?如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了在C#.net一个项目,我的数据库文件是一个Excel工作簿。由于连接字符串的位置是很难codeD在我的编码,有一个在我的系统上安装没有问题,但对于其他系统存在。

I have done a project in C#.NET where my database file is an Excel workbook. Since the location of the connection string is hard coded in my coding, there is no problem for installing it in my system, but for other systems there is.

有没有一种方法来提示用户该应用程序的安装完成后一次设置的路径?

Is there a way to prompt the user to set a path once after the setup of the application is completed?

我得到的答案是使用的app.config......谁能告诉这是什么App.config中,以及如何使用它在我这里的环境?

The answers I got was "Use App.Config"... can anyone tell what is this App.config and how to use it in my context here?

推荐答案

目前最简单的,在app.config与许多predefined配置提供自定义配置节的支持部分的XML文件。 A配置节是XML的使用意味着存储一些类型的信息架构的一个片段。

At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.

  • Overview (MSDN)
  • Connection String Configuration (MSDN)

设置可以使用内置的配置部分进行配置,如的ConnectionStrings 的appSettings 。您可以添加自己的自定义配置节;这是一个高级的主题,但非常强大的用于构建强类型的配置文件。

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.

Web应用程序通常有一个web.config,而Windows GUI /服务应用程序有一个app.config文件。

Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.

应用程序的配置文件继承全局配置文件,例如设置在machine.config。

Application config files inherit settings from global configuration files, e.g. the machine.config.

连接字符串有可以使用predefined模式。需要注意的是这个小片段实际上是一个有效的app.config(或web.config文件)文件:

Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

一旦你定义了你的app.config,你可以在code。使用读它的<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx">ConfigurationManager类。不要被冗长的MSDN的例子吓倒;它实际上很简单。

Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

书写到的app.config

经常变化的* .config文件通常不是一个好主意,但它听起来像你只是要执行一次性设置。

Writing to the App.Config

Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.

请参阅:更改连接字符串和放大器;重装的app.config在运行时介绍如何更新在运行时的* .config文件的的ConnectionStrings 部分。

See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.

请注意,理想情况下,你会从一个简单的安装程序进行这样的配置更改。

Note that ideally you would perform such configuration changes from a simple installer.

这篇关于什么是App.config中的C#.NET?如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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