加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 读取外部配置文件

发布时间:2020-12-15 03:46:02 所属栏目:百科 来源:网络整理
导读:我有一个执行FTP操作的c#.Net控制台应用程序. 目前,我在自定义配置部分指定设置,例如 ?xml version="1.0" encoding="utf-8" ?configuration configSections section name="ftpConfiguration" type="FileTransferHelper.FtpLibrary.FtpConfigurationSection,F
我有一个执行FTP操作的c#.Net控制台应用程序.
目前,我在自定义配置部分指定设置,例如
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ftpConfiguration" type="FileTransferHelper.FtpLibrary.FtpConfigurationSection,FileTransferHelper.FtpLibrary" />
  </configSections>

  <ftpConfiguration>
      <Environment name="QA">
        <sourceServer hostname="QA_hostname"
                      username="QA_username"
                      password="QA_password"
                      port="21"
                      remoteDirectory ="QA_remoteDirectory" />
        <targetServer downloadDirectory ="QA_downloadDirectory" />

      </Environment>
  </ftpConfiguration>

</configuration>

我想在命令行中指定一个外部配置文件.

然而!!!…

我只是意识到上述“FtpConfiguration”部分并不真正属于应用程序的app.config.我的最终目标是,我将有许多计划的任务执行我的控制台应用程序,如下所示:

FileTransferHelper.exe -c FtpApplication1.config
FileTransferHelper.exe -c FtpApplication2.config
...
FileTransferHelper.exe -c FtpApplication99.config

因此,我相信我已经走错了路径,我真正想要的是在我的自定义xml文档中阅读的内容,但继续使用System.Configuration来获取值…而不是读取XmlDocument并将其序列化获取节点/元素/属性. (虽然,如果有人可以向我显示一些简单的代码,我不会反对后者)

指针将不胜感激.谢谢.

更新:
我接受的答案是一个链接到另一个StackOverflow问题,重复这里与我的代码 – 下面正是我正在寻找 – 使用OpenMappedExeConfiguration来打开我的外部配置文件

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"D:DevelopmentFileTransferHelperConfigurationSampleInterface.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,ConfigurationUserLevel.None);

FtpConfigurationSection ftpConfig = (FtpConfigurationSection)config.GetSection("ftpConfiguration");

解决方法

如果要使用System.Configuration打开您的自定义文件,您可能需要检查这个帖子: Loading custom configuration files. Oliver以非常直接的方式指出它.

由于您想要读取通过命令行传递到应用程序的参数,您可能需要访问此MSDN的帖子:Command Line Parameters Tutorial.

如果您宁愿使用自定义方法,您可以通过几种方法来实现此目的.一种可能性是实现一个加载器类,并使用您的自定义配置文件.

例如,假设一个简单的配置文件如下所示:

spec1.config

<?xml version="1.0" encoding="utf-8"?>
<Settings>
    <add key="hostname" value="QA_hostname" />
    <add key="username" value="QA_username" />
</Settings>

一个非常简单的哈希式样(键值对)结构.

一个实现的解析器/读取器将看起来像这样:

private Hashtable getSettings(string path)
        {
            Hashtable _ret = new Hashtable();
            if (File.Exists(path))
            {
                StreamReader reader = new StreamReader
                (
                    new FileStream(
                        path,FileMode.Open,FileAccess.Read,FileShare.Read)
                );
                XmlDocument doc = new XmlDocument();
                string xmlIn = reader.ReadToEnd();
                reader.Close();
                doc.LoadXml(xmlIn);
                foreach (XmlNode child in doc.ChildNodes)
                    if (child.Name.Equals("Settings"))
                        foreach (XmlNode node in child.ChildNodes)
                            if (node.Name.Equals("add"))
                                _ret.Add
                                (
                                    node.Attributes["key"].Value,node.Attributes["value"].Value
                                );
            }
            return (_ret);
        }

同时,您仍然可以使用ConfigurationManager.AppSettings []从原始的app.config文件中读取.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读