在 Visual Studio 中,要在 app.config 文件中获取自定义部分的 IntelliSense,您需要执行以下步骤:
<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
<section name="MyCustomSection" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyCustomSection>
<add key="MyKey" value="MyValue" />
</MyCustomSection>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MyCustomSection"
elementFormDefault="qualified"
xmlns="http://schemas.microsoft.com/MyCustomSection"
xmlns:mcs="http://schemas.microsoft.com/MyCustomSection"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/MyCustomSection">
<xs:element name="MyCustomSection">
<xs:complexType>
<xs:sequence>
<xs:element name="add" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="key" type="xs:string" use="required" />
<xs:attribute name="value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?><configuration>
<configSections>
<section name="MyCustomSection" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyCustomSection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MyCustomSection.xsd">
<add key="MyKey" value="MyValue" />
</MyCustomSection>
</configuration>
如果您需要在代码中读取自定义部分的值,可以使用以下代码:
var configuration = ConfigurationManager.GetSection("MyCustomSection") as NameValueCollection;
var myValue = configuration["MyKey"];
请注意,这些步骤适用于 Visual Studio 和 C# 项目。对于其他编程语言和开发环境,您可能需要进行一些调整。
领取专属 10元无门槛券
手把手带您无忧上云