?????? ?? ????? C# ?? ????? ?? ???? ????????? ?? ??????????? ????? ?? ????? ??????? ??????? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 815786 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? ???? ?? Microsoft Visual Basic .NET ??????? ?? ???, ?????313405.
?? ???? ????? Microsoft .NET ????????? ????? ????????? ???????? ?? ???????? ???? ??::
  • System.Configuration
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? ????? ?? ?? ?? ???? ?????? ????????? ?????? ????? ??? ?? ????? ??? ??? ???? ??????? ?? ???? ??? ?? ??????????? (.config) ????? ??? ????? ??????? ???????? ???? ?? ??? ???? ????? ?? ?? ?????? ?? ?? ???? ????????? ?? ??????? ?? ?? ???? ????????? ???? ?????

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows 2000, Windows XP, Windows Server 2003, Windows Vista, or Windows Server 2008
  • Microsoft Visual C#
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ??????? ??????? ???? (XML)
  • .NET ??????????? ???????

Create a Console Application That Reads the Content of a Configuration File

You can store application settings in the configuration file that is associated with the application. Configuration files are saved in XML format.

TheSystem.Configuration??System.Collections.Specializednamespaces in the Microsoft .NET Framework include the necessary classes to retrieve information from a .NET application configuration file during run time.

????? ?? ??? ??? ?????? ??????????? ????? ?? ??????? ?? ????? ??? ?? ????? ???? ???? ????? ?????????, ?? ????? ?? ???? ????:
  1. Visual Studio .NET ?? Visual Studio 2005 ??????? ?????
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ????? ????,????? C#?? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????. ????????? ?? ???ConConfig. ???????? ??? ??, ????? C# ?????? ?? ????????? ??? ?? ?? ???? ????

    ???:Visual Studio .NET 2003, ????? ????????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????. ????????? ?? ???ConConfig. ???????? ??? ??, ????? C# ?????? ?? Class1 ??? ?? ?? ???? ????
  4. ?? ????????? ???? ?? ?????? Explorer ????? ??? ??? ??? ??? ?? ?????? ???? ??, ?? CTRL + ALT + L ????? ?????? ??????
  5. ?????? Explorer ???, ????????? ??? ?? ????-????? ????, ????? ????add?? ????-????? ????, ?? ???? ?????? ????.
  6. ??????? ???? ??? ????????? ???, ????? ?? ??? ????? ????XML ????.
  7. ??????????? ????? ???, ??????App.Config?? ????-????? ????, ?? ???? ???add.

    ???:Visual Studio .NET 2003, ????? ?????????.
  8. ?????/??? ?????? ??? ?????? ????? ????????? ???????? ??????? ???? ?? ??? ?? ???? ????????? ?? ??????????? ????? ?? ????? ?? ????? ????? ?? ???? ???<add></add>??? ????<appsettings></appsettings>??? ?????? ??????????? ????? ?? ??? ??? ???????? ?????/??? ????? ?? ??<add></add>????? ???<add></add>???? ?? ????? ?????? ??? ??:
    <add key="Key0" value="0" />
    ????? ???<appsettings></appsettings>?? ??? ???<add></add>?????? ?? ??? ??????????? ????? ?? ???<configuration></configuration>, ??????

    ?????? ?? ???, ????? ??????????? ????? ??? ????? ???<appsettings></appsettings>??? ?? ??? ?????/??? ?? ????? ?? ????????? ???? ??:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <appSettings>
          <add key="Key0" value="0" />
          <add key="Key1" value="1" />
          <add key="Key2" value="2" />
       </appSettings>
    </configuration>
    
  9. ?????? Explorer, ??? ??? ????? ????Program.cs??? ????? ????????? ????? Add the following statements to your code module.

    ???:These statements must appear before any other statements in the file.
    using System.Configuration;
    using System.Collections.Specialized;
  10. Add a reference to System.Configuration.dll. ??? ???? ?? ???, ????? ????? ?? ???? ????::
    1. ????? ????????????????? ??,?????? ??????.
    2. ??????????? ??????dialog box, click the.NET??? ?? ????? ????..
    3. Find and select the Component Name of System.Configuration.
    4. ????? ????,OK.
  11. To hold the value from a configuration file key in the<appsettings></appsettings>section of the configuration file, declare a string variable in the??????? ??? ??? ??????:
     string  sAttr ;
  12. To retrieve a value for a specified key from the<appsettings></appsettings>section of the configuration file, use theget?? ??? ????AppSettings?? ???ConfigurationManager????? TheConfigurationManagerclass is in theSystem.Configuration??? ????? ??? ??AppSettings.Getmethod receives a string input parameter that contains a key, the application retrieves the value that is associated with the key.

    The following code retrieves the value for theKey0??????? ?? ?????? ??????????? ????? ??? The code then places this value in thesAttrstring variable. If a key does not exist for this value, nothing is stored insAttr.
    sAttr = ConfigurationManager.AppSettings.Get("Key0");
  13. To display the value that the application retrieves in the Console window, use Console.WriteLine as follows:
    Console.WriteLine("The value of Key0 is "+sAttr);
  14. ???? ?? ??? ?? ?????? ?? ????? ?? ???? ???AppSettingsproperty to retrieve all the key/value pairs in the<appsettings></appsettings>???? ?? ?? ????? ????AppSettings???, ????????? ???? ??? ??????? ?????/??? ?? ?????? ?? ????? ??? ???????? ??? ??NameValueCollection??????:. TheNameValueCollectioncontains key/value entries for each key that the application retrieves. TheNameValueCollectionclass is in theSystem.Collections.Specialized??? ????? ???
    NameValueCollection sAll ;
    sAll = ConfigurationManager.AppSettings;
  15. TheAllKeys?? ???NameValueCollectionreferences a string array that has an entry for each key that the application retrieves. ????? ???foreachconstruction to iterate through theAllKeysarray to access each key that the application retrieves. Each key entry inAllKeysis a string data type.

    Inside theforeachconstruction, useConsole.WriteLineto display the key and its associated value in the Console window. The current key that the application processes is in "s". Use this as an index in thesAllNameValueCollectionto obtain its associated value.
       foreach (string s in sAll.AllKeys)
          Console.WriteLine("Key: "+ s + " Value: " + sAll.Get(s));
       Console.ReadLine();

??? ??????? ?????

using System;
using System.Configuration;
using System.Collections.Specialized;

namespace ConConfig
{
    class Program
    {
        static void Main(string[] args)
        {
            string sAttr;

            // Read a particular key from the config file            
            sAttr = ConfigurationManager.AppSettings.Get("Key0");
            Console.WriteLine("The value of Key0: " + sAttr);

            // Read all the keys from the config file
            NameValueCollection sAll;
            sAll = ConfigurationManager.AppSettings;

            foreach (string s in sAll.AllKeys)
                Console.WriteLine("Key: " + s + " Value: " + sAll.Get(s));
           Console.ReadLine();
       }
    }
}
???:?? ??? .NET Framework 2.0 ?? ?????? ?? ??? ??? ??? ?? .NET Framework 1.0 ?? .NET Framework 1.1 ????? ?? ??? ???, ?? ??? ?????????? ?????ConfigurationManager???? ?? ??? ????ConfigurationSettings.

??????????? ????? (ConConfig.exe.config) ?? ???? ?? ???? ????

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
      <add key="Key0" value="0" />
      <add key="Key1" value="1" />
      <add key="Key2" value="2" />
   </appSettings>
</configuration>

???? ???? ?? ??? ???

??? ?? ????? ?? ??? F5 ?????.. ?????? ????? ?? ?????/??? ?? ????? ?? ????????? ???? ?????<appsettings></appsettings>?????? ??????????? ????? ?? ??? ??? follows ??????:
The value of Key0: 0
Key: Key0 Value:0
Key: Key1 Value:1
Key: Key2 Value:2
		

?????? ??????

  • ??????????? ????? ?? XML ?????? ??? ????? ??? ??? ????????? ???? ?? ?? ??? XML ???????? ?????? ?? ???? ????? ??? ???? ?? XML ???-?????? ??? ??? XML ?? formed ???? ??, ?? ??? ??? ???? misspelled ??, ???? ???? System.Configuration.Configuration ????? ??????? ???? ???

    ?????? ?? ???, ??? ?? ?????? ?? ????? ??????? ??<add></add>??? ?????? "K" ?? ???? ???? ??????? "k," ???? ???? ?? ???<appsettings></appsettings>?????? ?? ??? ??? ????? ???? ??<appsettings></appsettings>(??? ?????? "???" ?? ??????? ?? ???? ?? ??? "??"), ???? ?? ?????? ????? ??????? ???? ???
  • ??????????? ????? ?? ???? ?????? ????????? ?? ??? ??????? ??? ????? ???? ??????
  • ??????????? ????? ??? ?? ??? ?? ????? ???????? ?? ????? ???? ?????? ??:
    ApplicationName.ApplicationType.Config
    ????ApplicationName????????? ?? ??? ??ApplicationType?????????. (?????? ?? ???, exe) ?? ?????? ??, ?? .Config ?? ?????? ??????? ???

??????

???? ??????? ?? ???, ????? Microsoft ?????? ????? ??????? (MSDN) ??? ????:
ConfigurationSettings.AppSettings ???
(vs.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.configuration.configurationsettings.appsettings .aspx
System.Configuration ????????
(vs.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.configuration .aspx

???

???? ID: 815786 - ????? ???????: 05 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbsweptvs2008 kbcollections kbcollectionclass kbconfig kbnamespace kbhowtomaster kbprogramming kbmt KB815786 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:815786

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com