What is App.config in C#.NET? How to use it?

前端 未结 6 2009
情深已故
情深已故 2020-11-22 15:00

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

6条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 15:27

    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)

    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 applications typically have a web.config, while Windows GUI/service applications have an app.config file.

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

    Reading from the App.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:

    
    
           
            
        
    
    

    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;
    

    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.

    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.

    Location of the App.Config at Runtime

    Q: Suppose I manually change some in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?

    A: When you compile an application, its app.config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a "text.exe.config" in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.

    1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.

    .NET Core

    New configuration options were introduced with .NET Core. The way that *.config files works does not appear to have changed, but developers are free to choose new, more flexible configuration paradigms.

    • Configuration in ASP.NET Core
    • Essential .NET - Configuration in .NET Core

提交回复
热议问题