App.config 找不到我的元素
App.config can't find my element
我有一个简单的 app.config
文件,它没有完全通过 IDE 的检查。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="XmlRoot" type="System.String"/>
</configSections>
<connectionStrings>
<omitted/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<XmlRoot>
<add key="relativepath" value=""/>
</XmlRoot>
</configuration>
我将 <section name="XmlRoot" type="System.String" />
部分添加到配置中,然后尝试在此处定义名称和密钥:<add key="relativepath" value=""/>
。但出于某种原因,IDE 给了我以下消息:
我很少使用 app.config 文件,所以它可能只是一个菜鸟错误。如何让它识别我的标签?
如果只是简单的字符串,可以使用AppSettings来实现。
<configuration>
<appSettings>
<add key="relativepath" value="mypath" />
</appSettings>
</configuration>
像这样从 C# 访问它:
string path = ConfigurationManager.AppSettings["relativepath"]);
您可能需要使用 appSettings 部分
<configuration>
<appSettings>
<!-- and Here you define the key and the value like you do-->
<add key="relativepath" value="path"/>
</appSettings>
</configuration>
然后在你的代码中写这样的东西来阅读它。
String path = ConfigurationManager.AppSettings["relativepath"].ToString();
希望对你有用。
:)
尝试将类型更改为 System.Configuration.NameValueSectionHandler
这是我定义我的部分的方式:
<configSections>
<sectionGroup name="MySettings">
<section name="serverConfiguration" type="System.Configuration.NameValueSectionHandler"></section>
<section name="sqlSettings" type="System.Configuration.NameValueSectionHandler"></section>
</sectionGroup>
</configSections>
...
<MySettings>
<sqlSettings>
<add key="sqlTransactionTimeOut" value="0" />
</sqlSettings>
<serverConfiguration>
<add key="resolveDnsAsync" value="true" />
</serverConfiguration>
</MySettings>
我有一个简单的 app.config
文件,它没有完全通过 IDE 的检查。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="XmlRoot" type="System.String"/>
</configSections>
<connectionStrings>
<omitted/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<XmlRoot>
<add key="relativepath" value=""/>
</XmlRoot>
</configuration>
我将 <section name="XmlRoot" type="System.String" />
部分添加到配置中,然后尝试在此处定义名称和密钥:<add key="relativepath" value=""/>
。但出于某种原因,IDE 给了我以下消息:
我很少使用 app.config 文件,所以它可能只是一个菜鸟错误。如何让它识别我的标签?
如果只是简单的字符串,可以使用AppSettings来实现。
<configuration>
<appSettings>
<add key="relativepath" value="mypath" />
</appSettings>
</configuration>
像这样从 C# 访问它:
string path = ConfigurationManager.AppSettings["relativepath"]);
您可能需要使用 appSettings 部分
<configuration>
<appSettings>
<!-- and Here you define the key and the value like you do-->
<add key="relativepath" value="path"/>
</appSettings>
</configuration>
然后在你的代码中写这样的东西来阅读它。
String path = ConfigurationManager.AppSettings["relativepath"].ToString();
希望对你有用。
:)
尝试将类型更改为 System.Configuration.NameValueSectionHandler
这是我定义我的部分的方式:
<configSections>
<sectionGroup name="MySettings">
<section name="serverConfiguration" type="System.Configuration.NameValueSectionHandler"></section>
<section name="sqlSettings" type="System.Configuration.NameValueSectionHandler"></section>
</sectionGroup>
</configSections>
...
<MySettings>
<sqlSettings>
<add key="sqlTransactionTimeOut" value="0" />
</sqlSettings>
<serverConfiguration>
<add key="resolveDnsAsync" value="true" />
</serverConfiguration>
</MySettings>