??? ??? ????? ????????? ??????? ?????????? ?? ??? ????? ????? ???????? Visual C#

?????? ????????? ?????? ?????????
???? ???????: 815786 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual Basic .NET ?? ??? ???????? ???? 313405.
???? ??? ??????? ??? Microsoft .NET Framework ??????? ????? ??? "????? ??????":
  • System.Configuration
????? ???? | ?? ????

?? ??? ??????

??????

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

???????

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

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

???? ????? ??????? ????????? ?? ??? ??????? ???????? ?? ???????. ??? ??? ????? ??????? ?? XML ?????.

????? System.Configuration ?????? ??????? System.Collections.Specialized ?? Microsoft .NET Framework ???? ???????? ???????? ??????? ?? ??? ????? ????? .NET ????? ??? ???????.

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

    ?????? ?? Visual Studio .NET 2003 ? ???? ??? Visual C# ?????? ??? ????? Project ?? ???? ??? ????? ???? ?????? ??? ?????. ??? ??????? ConConfig. ????? ???????? ???? Visual C# ?????? ??? ???? Class1.
  4. ???? ?? ???? ???? "?????? ??????". ??? ??? ??? ???? ???? ?????? ???????? CTRL + ALT + L.
  5. ?? "?????? ??????" ???? ??? ?????? ?????? ??? ??? ??????? ???? ??? ????? ?? ???? ??? ???? ????.
  6. ?? ??????? ????? ???? ???? ? ???? ?????? ??? XML.
  7. ?? ???? ???? ??? ???? App.config ??? ?? ???? ??? ?????.

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

    ??? ???? ??????? ????? ??? ??????? ??????? <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. ?? "?????? ??????" ???? ????? ??????? ??? Program.cs ???? ???? ????????? ????????. ?? ?????? ???????? ??????? ??? ?????? ??????? ????????? ???????? ?????? ??.

    ?????? ??? ?? ???? ??? ???????? ??? ??? ?????? ???? ?? ?????.
    using System.Configuration;
    using System.Collections.Specialized;
  10. ????? ???? ??? System.Configuration.dll. ?????? ????? ???? ??????? ???????:
    1. ?? ??????? ??????? ???? ??? ????? ????.
    2. ?? ???? ?????? ????? ??????? ???? ??? ????? ??????? .NET.
    3. ??? ??? ???? System.Configuration ????? ??.
    4. ???? ??? ?????.
  11. ???????? ?????? ?? ????? ??? ????? ?? <appsettings> ?????? ?? ??? ??????? ????? ????? ????? ?? ????? ??????? ??? ???:
     string  sAttr ;
  12. ??????? ???? ????? ???? ?? <appsettings> ?????? ?? ??? ??????? ??????? ????? ?????? ??? ????? AppSettings ??? ConfigurationManager. ????? ??? ConfigurationManager ?? ????? ????? System.Configuration. ????? ????? ????? AppSettings.Get ????? ????? ????? ???? ????? ??? ????? ? ????? ??????? ?????? ???????? ???????.

    ??????? ???????? ???????? ??????? ?????? ????? Key0 ?? ??? ??????? ????????. ???????? ???????? ?? ??? ??? ?????? ?? ????? ????? sAttr. ?? ???? ??? ???? ????? ???? ?????? ? ??? ????? ?? ?? ?? sAttr.
    sAttr = ConfigurationManager.AppSettings.Get("Key0");
  13. ???? ???? ???? ???????? ??????? ?? ???? ???? ?????? ?????? Console.WriteLine ??? ???:
    Console.WriteLine("The value of Key0 is "+sAttr);
  14. ????? ??????? ???? ???? ??? ????? AppSettings ???????? ???? ????? ?????/???? <appsettings> ??????. ??? ??????? ??????? AppSettings ??????? ????? ???? ????? ?????/???? ????????. ??? ????? ??? ??????? ?? ??? NameValueCollection. ????? NameValueCollection ??? ??????? ?????/???? ??? ????? ????? ???????. ??? NameValueCollection ?????? ?? ????? ????? System.Collections.Specialized.
    NameValueCollection sAll ;
    sAll = ConfigurationManager.AppSettings;
  15. ???? ??????? AllKeys ?? NameValueCollection ???? ????? ??? ????? ??? ????? ????? ???????. ?????? ???? foreach ????? ???? ?????? AllKeys ?????? ??? ?? ????? ???? ???????? ???????. ?? ????? ????? ?? AllKeys ??? ?????? ?????.

    ???? ???? foreach ??????? Console.WriteLine ???? ??????? ??????? ???????? ?? ???? ?????? ????. ??? ??????? ?????? ???? ????? ??????? "s". ??????? ??? ???? ?? sAllNameValueCollection ?????? ??? ?? ???????? ????.
       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> ??? ???? ??????? ???????? ??? ???:
The value of Key0: 0
Key: Key0 Value:0
Key: Key1 Value:1
Key: Key2 Value:2
		

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

  • ??? ??? ??? ????? ?????? XML. ???? ?? ????? ???? ????? ???? ???? XML. ???? ?? XML ???? ????? ??????. ??? ?? ??? ????? XML ????? ??? ?? ??? ????? ?? ????? ???? ???? ??????? System.Configuration.Configuration.

    ??? ???? ??????? ??? ??? ?????? ??? ??????? <add> ?????? ?? ?????? ????? "?" ????? ?? ???? ????? "? ?" ?? ??? ??? <appsettings> ???? ?????? <appsettings> (?? ????? ???? "A" ????? ?? ????? "a") ? ???? ????? ???.
  • ??? ?? ??? ??? ??? ??????? ?? ??? ?????? ??? ??????? ??????? ??.
  • ??? ?? ?????? ???? ?????? ?????? ??? ??? ???????:
    ApplicationName. .config ApplicationType
    ??? ???? ApplicationName ?? ??? ??????? ApplicationType ?? ??? ??????? (??? ???? ??????? & .exe) ??? .config ????? ????????.

?????

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

???????

???? ???????: 815786 - ????? ??? ??????: 13/???/1429 - ??????: 3.1
????? ???
  • 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
????? ??????: 
kbmt kbsweptvs2008 kbcollections kbcollectionclass kbconfig kbnamespace kbhowtomaster kbprogramming KB815786 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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