嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及114嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��



           place their piled output。 As a habit�察�I often make all projects build to a central directory。  

           You can set the output directory in the pile tab of a project¨s Properties window�察�as shown  

           in Figure 12´2。 



           Figure 12´2。 Setting the build output path to a mon path �─�。bin�� 



                You need to set the Build Output Path field to a mon location for all projects。 When  

           you do that�察�a build will result in a directory structure like that shown in Figure 12´3 ��in the next  

           section��。 With all of the files in a mon directory�察�running the dynamic´loading routines  

           bees trivial。  


´´´´´´´´´´´´´´´´´´´´´´Page 341´´´´´´´´´´´´´´´´´´´´´´´

           CH AP T E R   1 2   *    L E AR N IN G   AB O U T   AP P L I CAT I ON   CO N F IG U R AT IO N   A N D   D Y N A M IC   L O AD IN G 319 



Defining and Processing a Configuration File 



A configuration file is a file that contains information about how a program should behave。  

Configuration files by themselves do not control how a program behaves。 For a configuration  

file to influence the behavior of a program�察�the program needs to read and act on the informa

tion in the configuration file。 

      Using a configuration file is not as easy as you may think it is。 Yes�察�it is easy to define a  

configuration file and read the configuration file。 What is difficult is how to define where  

the configuration file is located。  

      Suppose you have an application that is installed on the hard disk�察�and the application  

must make an assumption about where the configuration file is stored。 One assumption could  

be the root directory of the C�此�drive。 Yet that could be incorrect�察�because some puters don¨t  

have C�此�as the root drive。  

       solves this problem in an interesting way�此�whatever the executing application is called�察 �

the configuration file has the same name with a  。config extension。 Figure 12´3 shows an example�察 �

in which the console application is CallRuntimeImplementation。exe。 The configuration file is  

CallRuntimeImplementation。exe。config。 A configuration file and the assembly that it relates to  

are located in the same directory。 



Figure 12´3。 A console application and its  associated configuration file 



*Note  Naming the configuration file using a bination of the assembly¨s application name with the  

 。config extension is an example of a convention architecture。 



      In this section�察�you¨ll see how to write a configuration file and then reference it within an  

application。 This setup will use the configuration file whenever a request for a certain piece of  

functionality is made and supply the exact location of the assembly。 



Creating an XML´Based Configuration File 



The configuration file contains Extensible Markup Language ��XML��。 XML is a way of structuring  

a text file。  

      Consider the following text structure using spaces。 


´´´´´´´´´´´´´´´´´´´´´´Page 342´´´´´´´´´´´´´´´´´´´´´´´

320       CH AP T E R   1 2   *    L E A R N I N G   A B OU T   A PP L I CA TI O N   CO N F I G U R AT IO N   AN D   D Y N A M I C  L O AD I N G 



           First Element 

               Second Element 

               Third Element 



                First Element is a parent�察�and  Second Element and Third Element are subelements of the  

           parent。 The structuring of the parent and child elements is fragile�察�to say the least。 We need a  

           more robust way of structuring the data�察�which is where XML es in。 Think of XML as having  

           the ability to define folders and folders within folders。 

                Let¨s define the XML that makes up a  application configuration file。 All  applica

           tion configuration files can be created using Visual Basic Express�察�as follows�此�



                1。  Right´click the CallRuntimeImplementation project and select Add  New Item。 



                2。  Select Text File。 



                3。  Name the file App。config and click Add。 The App。config file is created。 



                It is fine to leave the name as is�察�because when your  application is piled�察�the   

           application configuration file will be renamed and placed beside the  application。 



           *Note   application configuration files apply to assemblies。 Thus�察�if an assembly uses the config

           uration API�察�the configuration file applied will be the one associated with the calling application。  



                Open the new App。config file and add the following XML�此�



            

             

            



                The first line is called an XML declaration�察�and it can specify the encoding that will be used  

           ��plus other information�察�which we won¨t need to concern ourselves with here��。 The second and  

           third lines define a root XML node ��also called an XML element��。 Think of a root XML node as  

           analogous to a root folder where all of the other folders will be stored。 

                The root node is started with the identifier configuration surrounded by angle brackets。  

           The node is terminated by the same identifier and angle brackets�察�with the addition of a slash  

           prefix to the identifier。 The only place where another XML node can be placed is between the  

           beginning and ending XML nodes。 For example�察�this XML is invalid�此�



            

            

            

            



                In the example�察 �is started inside the declaration of �察�but its termi

           nation�察。察�is outside the declaration of 。  


´´´´´´´´´´´´´´´´´´´´´´Page 343´´´´´´´´´´´´´´´´´´´´´´´

          CH AP T E R   1 2   *    L E AR N IN G   AB O U T   AP P L I CAT I ON   CO N F IG U R AT IO N   A N D   D Y N A M IC   L O AD IN G 321 



Adding the Dynamic Loading Configuration Items 



After you¨ve added a configuration file�察�the next step is to add the elements that will be used by  

the dynamic loader。 The idea will be to define some abstract identifier that will be cross´referenced  

to a type and assembly。 Thus�察�to load Implementations1。Implementation�察�we¨ll use the identifier  

Impl1�察�type Implementations1。Implementation�察�and assembly Implementations1。dll。 Similarly�察 �

Impl2 will cross´reference to the type  Implementations2。Implementation and the assembly  

Implementations2。dll。  

     Modify the configuration file as shown here�察�adding the bolded code。 



 

 

   

     

   



 



     The XML node �察�which you could think of as adding the folder appSettings�察 �

contains XML nodes with the identifier 。 This node defines a section in the configuration  

file that contains application settings expressed as key/value pairs。 Each key/value pair is defined  

in the XML node using the XML attributes key and value。  

     Notice that the XML node  has a starting element�察�but not an ending element。 This is  

because the  node is terminated with an angle bracket prefixed with a slash。 This means  

that the ending element is not necessary。  

     The configuration file defines a key Assemblies and a value that contains a buffer of ma

separated identifiers。 The identifiers represent the identifier�察�type�察�and assembly of supported  

dynamically loaded assemblies。 

     The configuration file is plete�察�but will not influence the behavior of the application�察 �

because source code must be added to the application that reads the configuration file settings�察 �

as describ
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��