如何从 C# 读取 VB.NET 项目中的“app.config”文件

Posted

技术标签:

【中文标题】如何从 C# 读取 VB.NET 项目中的“app.config”文件【英文标题】:How to Read an "app.config" File in a VB.NET Project from C# 【发布时间】:2012-09-05 14:46:31 【问题描述】:

我有一个包含一个 VB.NET 项目和一个 C# 项目的解决方案。 VB.NET 项目是主要项目。它引用了用于用户管理服务的 C# 项目。两者都连接到同一个数据库。此数据库的连接字符串存储在 VB.NET 项目的 app.config 文件中。它还包含其他用户和应用程序级别设置。我可以使用以下代码行从 C# 内部的配置中读取连接字符串:

String connectionString = ConfigurationManager.ConnectionStrings["Kais2012.My.MySettings.ConnectionString"].ConnectionString;

配置文件中包含的其他应用程序级别属性之一称为“TeamId”。我已经编写了以下代码来从 C# 中读取它。

String teamId = ConfigurationManager.AppSettings["Kais2012.My.MySettings.TeamId"];

这不起作用,我不知道为什么。任何帮助将不胜感激。谢谢。

它不起作用的方式是teamId返回null。这是 app.config 文件的内容。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Kais2012.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Kais2012.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
    <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
        <EncryptedData>
            <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q4BpPIXSyqMvVyc15hB1wHmV8vLbG</CipherValue>
            </CipherData>
        </EncryptedData>
    </connectionStrings>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <userSettings>
        <Kais2012.My.MySettings>
            <setting name="AutoNavigation" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="UseOsk" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="AutoHideOsk" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="StretchReviewColumns" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="DiscountSkipped" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="AutoDelay" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="MinAutoDelay" serializeAs="String">
                <value>1000</value>
            </setting>
        </Kais2012.My.MySettings>
    </userSettings>
    <applicationSettings>
        <Kais2012.My.MySettings>
            <setting name="DummyColumn" serializeAs="String">
                <value>CreationTime</value>
            </setting>
            <setting name="PublisherDBName" serializeAs="String">
                <value>kais_final</value>
            </setting>
            <setting name="PublicationName" serializeAs="String">
                <value>pbl_kais_WAKHUTU-PC</value>
            </setting>
            <setting name="SubscriptionDBName" serializeAs="String">
                <value>KAISEQ_USER001</value>
            </setting>
            <setting name="SubscriberServerName" serializeAs="String">
                <value>USER001</value>
            </setting>
            <setting name="PublisherServerName" serializeAs="String">
                <value>WAKHUTU-PC</value>
            </setting>
            <setting name="PublisherLogin" serializeAs="String">
                <value>sa</value>
            </setting>
            <setting name="PublisherPassword" serializeAs="String">
                <value>xxxxxx</value>
            </setting>
            <setting name="UploadToServer" serializeAs="String">
                <value>1</value>
            </setting>
            <setting name="IpAddress" serializeAs="String">
                <value>172.23.14.94</value>
            </setting>
            <setting name="DownloadFromServer" serializeAs="String">
                <value>2</value>
            </setting>
            <setting name="UploadAndDownload" serializeAs="String">
                <value>3</value>
            </setting>
            <setting name="SubscriberPassword" serializeAs="String">
                <value>xxxxxx</value>
            </setting>
            <setting name="PublishingSubscriberServerName" serializeAs="String">
                <value>SUP01</value>
            </setting>
            <setting name="PublishingSubscriberLogin" serializeAs="String">
                <value>sa</value>
            </setting>
            <setting name="PublishingSubscriberPassword" serializeAs="String">
                <value>xxxxxx</value>
            </setting>
            <setting name="PublishingSubscriberDBName" serializeAs="String">
                <value>KAISEQ_SUP01</value>
            </setting>
            <setting name="publishingSubscriberPublicationName" serializeAs="String">
                <value>pbl_kais_KAISEQ_SUP01</value>
            </setting>
            <setting name="SubscriberLogin" serializeAs="String">
                <value>sa</value>
            </setting>
            <setting name="TeamId" serializeAs="String">
                <value>2</value>
            </setting>
            <setting name="DevMode" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="DevUsername" serializeAs="String">
                <value>admin</value>
            </setting>
            <setting name="DevPassword" serializeAs="String">
                <value>admin</value>
            </setting>
        </Kais2012.My.MySettings>
    </applicationSettings>
</configuration>

【问题讨论】:

您能否发布您的 app.config 中的appSettings 部分。 应用设置是"TeamId"还是"Kais2012.My.MySettings.TeamId"?发布 app.config 的这两个部分可能会有所帮助。 这些在配置文件中看起来如何(我们不需要值,只需要结构)? 怎么不行?您是否收到编译器错误?你有例外吗?你一无所获? 配置文件是相对于“应用程序”读取的,因此请确保您运行的项目包含 web/app.config 文件 【参考方案1】:

您可以在 C# 项目中将设置标记为公共(设置属性窗格中的“访问修饰符”),然后您可以从 vb 项目中访问它(不要忘记添加引用)。

 Dim settings = New ClassLibraryCSharp.Properties.Settings
 dim con = settings.ConnectionString

编辑: 刚刚看到您的问题是针对另一个方向的(在 vb 项目中设置,在 c# 项目中阅读)。但是你也可以在另一个方向上这样做。

【讨论】:

【参考方案2】:
ConfigurationManager.AppSettings[""]

具体指配置节&lt;appSettings&gt;,而不是名为&lt;applicationSettings&gt; 的自定义配置节组。将您的配置移动到正确的部分或实现自定义ConfigurationSection 并使用:

ConfigurationManager.GetSection()

【讨论】:

【参考方案3】:

使用这个:

System.Configuration.ConfigurationManager.AppSettings[""]

【讨论】:

以上是关于如何从 C# 读取 VB.NET 项目中的“app.config”文件的主要内容,如果未能解决你的问题,请参考以下文章

vb.net如何与excel建立连接,并从里面查询并读取数据?

C#/VB.NET 获取Excel中的表单控件

如何将公共事件从 c# 转换为 vb.net [关闭]

vb.net怎么从DataSet中读取一行的数据

将 SQL 命令从 vb .net 转换为 C#

如何在带有 VB.net GUI 的 C++ OpenCV 项目中使用 C# 库?