简体   繁体   English

如何从 C# 中的服务器位置获取 app.config 值

[英]How to app.config value from server location in c#

I wanna read/write (and use) application's configuration file in program我想在程序中读/写(和使用)应用程序的配置文件

app.config of server服务器的 app.config

  <configSections>
   <section />
  </configSections>
    <connectionStrings>
  <add name="catalogue" connectionString="Data Source=abc\_SQLSERVER;Initial 
  Catalog=Catalogue;Integrated Security=False;Persist Security Info=False; user ID=sa;password=****;" 
    providerName="System.Data.SqlClient"/>
      </connectionStrings>
        <dataConfiguration defaultDatabase="Catalogue"/>
     <startup>

  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
 <appSettings>


     </appSettings>
    </configuration>

I want to use the app.config file which is present at the following location on some other server location '\\abc\\BaseSoftwares\\hanah\\abc.exe.config' instead of local configuration.我想使用 app.config 文件,该文件位于其他服务器位置 '\\abc\\BaseSoftwares\\hanah\\abc.exe.config' 上的以下位置,而不是本地配置。 Using ConfigurationManager.使用配置管理器。 OpenExeConfiguration OpenExe配置

    string pathString = System.IO.Path.Combine(CentralizedPath, "abc.exe.config"); 
    Configuration config = ConfigurationManager.OpenExeConfiguration(pathString);

    ConnectionStringsSection section = config.GetSection("connectionStrings") as 
         ConnectionStringsSection;

section is not returning the connection string of server app config which is [catalogue].部分不返回服务器应用程序配置的连接字符串,即 [目录]。 Anyone who could help me with this?谁能帮我解决这个问题?

  1. ConnectionStringsSection().ConnectionStrings gives the collection of all connection strings in your config. ConnectionStringsSection().ConnectionStrings 提供配置中所有连接字符串的集合。

  2. Iterate through to get the required result.迭代以获得所需的结果。

ConnectionStringSettingsCollection collection= config.ConnectionStringsSection().ConnectionStrings;

foreach(ConnectionStringSettings css in collection)
{
   string output= css.Name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM