?????? ?? Visual Basic .NET ?? Visual Basic 2005 ?? ????? ?? ???? ????????? ?? ??????????? ????? ?? ????? ??????? ??????? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 313405 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
???? Microsoft Microsoft Visual C# .NET ??????? ?? ??? ?? ???? ??, ?????815786.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

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


??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows 2000, Microsoft Windows XP, ?? Microsoft Windows Server 2003
  • Microsoft Visual Studio .NET (2002) ?? Microsoft Visual Studio .NET (2003) ?? Microsoft Visual Studio 2005
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ??????? ??????? ???? (XML)
  • .NET ??????????? ???????

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

You can store application settings within 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.

To create a console application that reads the contents of an associated configuration file during run time, follow these steps:
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Create a new Console Application project named ConConfig in Visual Basic .NET or Visual Basic 2005. Visual Basic .NET ?? Visual Basic 2005 Module1 ?? ???????? ??? ?? ??? ?? ??? ??????? ????? ???
  3. ConConfig ?? ????-????? ????????????????????, ?? ??? ?????add?? ????-????? ????, ?? ???? ????? ???? ??? ?????.

    ???:Visual Studio 2005, ??? ConConfig ????-?????????????????????, ?? ??? ?????add?? ????-????? ????, ?? ???? ?????? ????.
  4. ??????? ???? ??? ?????????? ????? ???, ?? ??? ????????????? ??????????? ????????????? ?? ???, ????? ?????????, ?? ???? ??? ????????? ?? ??? App.config ????? ???????

    ???:????????? Visual Studio .NET ?? Visual Studio 2005 IDE ????? App.Config ??????? ???? ???????? ????? ????? ?????? ?? ?? ??? ??? ?? ??? ????? ??<applicationname></applicationname>.<application type=""></application>.Config???????
  5. ?????/??? ?????? ??? ?????? ????? ????????? ???????? ??????? ???? ?? ??? ?? ???? ????????? ?? ??????????? ????? ?? ????? ?? ????? ????? ?? ???? ???<add></add>??? ????<appsettings></appsettings>??? ?????? ??????????? ????? ?? ??? ??? ???????? ?????/??? ????? ?? ??<add></add>????? ???<add></add>???? ?? ????? ?????? ??? ??:
    <add key="theKey" value="theValue" />
    						
    ????? ???<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>
    					
  6. ?????? Explorer, ??? ??? ????? ????Module1.VBModule1 ??? ????? ????????? ????? ???? ??? ??????? ?? ??? ????? ????? ??????:

    ???:These statements must appear before any other statements in the file.
    Imports System.Configuration
    Imports System.Collections.Specialized
    					
  7. ??????? ?????procedure, dimension a string variable to hold the value from a configuration file key in the<appsettings></appsettings>section of the configuration file:
    Dim sAttr As String
    					
  8. To retrieve a value for a specified key from the<appsettings></appsettings>section of the configuration file, use theAppSettings?? ??? ????ConfigurationSettings????? TheConfigurationSettingsclass is in theSystem.Configuration??? ????? ??? ??AppSettingsmethod 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??????? ?? ?????? ??????????? ????? ??? ??? ??? ?? ??? ??? ???? ???????? ??sAttr. ??? ?? ??? ?? ??? ??? ????? ????? ???? ????? ???? ????? ???????? ??sAttr.
    sAttr = ConfigurationSettings. AppSettings("Key0")
    					
  9. ?????? ????? ??? ????????? retrieves ??? ?? ????????? ???? ?? ??? ?????Console.WriteLine:
    Console.WriteLine("The value of Key0: " & sAttr)
    					
  10. ???? ?? ??? ?? ?????? ?? ????? ?? ???? ???AppSettings??? ??? ?????/??? ?? ????? ?? ??????? ???? ?? ??? ???<appsettings></appsettings>???? ??? ?? ???? ????????? ??? ?? ???????? ?? ????? ???? ???AppSettings???, ????????? ???? ??? ??????? ?????/??? ?? ?????? ?? ????? ??? ???????? ??? ??NameValueCollectiontype, which contains key/value entries for each key that the application retrieves. TheNameValueCollectionclass is in theSystem.Collections.Specialized??? ????? ???
    Dim sAll As NameValueCollection
    sAll = ConfigurationSettings.AppSettings()
    					
  11. TheAllKeys?? ???NameValueCollectionreferences a string array that has an entry for each key that the application retrieves. ????? ??????? ?? ???construction to iterate through theAllKeysarray to access each key that the application retrieves. Each key entry inAllKeysis a string data type.
    Dim s As String
    For Each s In sAll.AllKeys
    					
  12. Inside the???????? ?? ???construction, 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.

    For example, if the application processes theKey0key, sAll("Key0") retrieves its associated value. TheConsole.Readlinestatement pauses the console. You can press ENTER to end the application.
        Console.WriteLine("Key: " & s & " Value: " & sAll(s))
    Next
    Console.Readline
    					

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

Press the F5 key to run the code. The Console window should display the key/value pairs from the<appsettings></appsettings>section of the associated configuration file as follows:
The value of Key0: 0
Key: Key0 Value:0
Key: Key1 Value:1
Key: Key2 Value:2
					

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

Imports System.Configuration
Imports System.Collections.Specialized

Module Module1

    Sub Main()
        Dim sAttr As String

        sAttr = ConfigurationSettings.AppSettings("Key0")
        Console.WriteLine("The value of Key0: " & sAttr)
        
        Dim sAll As NameValueCollection
        sAll = ConfigurationSettings.AppSettings()

        Dim s As String
        For Each s In sAll.AllKeys
            Console.WriteLine("Key: " & s & " Value: " & sAll(s))
        Next
		  Console.ReadLine()
    End Sub

End Module
				

Complete Configuration File Listing (ConConfig.exe.config)

<configuration>
	<appSettings>
		<add key="Key0" value="0"/>
		<add key="Key1" value="1"/>
		<add key="Key2" value="2"/>
	</appSettings>
</configuration>
				

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

  • The configuration file is saved in XML format. Make sure that you follow all XML syntax rules. ??? ???? ?? XML ??? ?????? ??? ??? XML ?? formed ???? ??, ?? ??? ??? ???? misspelled ??, ???? ???? System.Configuration.Configuration ????? ??????? ???? ???

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

??????

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

???

???? ID: 313405 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbconfig kbhowtomaster kbmt KB313405 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:313405

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

 

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