读取外部配置文件 [英] reading external configuration file

查看:205
本文介绍了读取外部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有执行FTP操作的C#.NET控制台应用程序。
目前,我在自定义配置节指定的设置,如:

 <?XML版本=1.0编码=UTF-8>?; 
<结构>
< configSections>
<节名称=ftpConfigurationTYPE =FileTransferHelper.FtpLibrary.FtpConfigurationSection,FileTransferHelper.FtpLibrary/>
< / configSections>

< ftpConfiguration>
<环境NAME =QA>
< sourceServer主机名=QA_hostname
的用户名=QA_username
密码=QA_password
端口=21
remote目录=QA_remoteDirectory/> ;
< targetServer downloadDirectory =QA_downloadDirectory/>

< /环境与GT;
< / ftpConfiguration>

< /结构>



我想指定,在命令行中,外部配置文件。



但!!! ...



我刚刚意识到的是,上述FtpConfiguration部分没有真正在应用程序的归属app.config中。我的最终目标是,我将有一个执行我的控制台应用程序,很多这样的计划任务:

  FileTransferHelper.exe -c FtpApplication1。配置
FileTransferHelper.exe -c FtpApplication2.config
...
FileTransferHelper.exe -c FtpApplication99.config

因此,我相信我已经走上错误的道路,我真正想要的东西是我的自定义XML文档中读取,但继续使用System.Configuration得到的值.. ,而不是读一个XmlDocument和序列化它来获得节点/元素/属性。 (虽然我不反对后者,如果有人能告诉我一些简单的代码)



指针将不胜感激。谢谢



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

  ExeConfigurationFileMap configFileMap =新ExeConfigurationFileMap(); 
configFileMap.ExeConfigFilename = @D:\Development\FileTransferHelper\Configuration\SampleInterface.config

配置配置= ConfigurationManager.OpenMappedExeConfiguration(configFileMap,ConfigurationUserLevel.None);

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


解决方案

如果你想使用System.Configuration打开你的自定义文件,你可能要检查这个帖子: 加载自定义配置文件 。奥利弗钉在一个非常简单的方法。



既然你想读传递给通过命令行应用程序参数,你可能要拜访这个MSDN后 命令行参数教程



如果你想用一个自定义的方法,还有你可以做到这一点的一些方法。其中一个可能性是实现一个loader类,并使用您的自定义配置文件



例如,假设一个简单的配置文件看起来像这样:



spec1.config

 < XML版本= ?1.0编码=UTF-8> 
<设置>
<添加键=主机名价值=QA_hostname/>
<添加键=用户名值=QA_username/>
< /设置>

有一个非常简单的,哈希表样(键值对)结构。



这是实现然后解析器/读者会是这个样子:

 私人哈希表的getSettings(串路径)
{
Hashtable的_RET =新的Hashtable();
如果(File.Exists(路径))
{
StreamReader的读者=新的StreamReader

新的FileStream(
路径,
的FileMode 。开,
FileAccess.Read,
FileShare.Read)
);
XmlDocument的DOC =新的XmlDocument();
串xmlIn = reader.ReadToEnd();
reader.Close();
doc.LoadXml(xmlIn);
的foreach(在doc.ChildNodes XmlNode的孩子)
如果(child.Name.Equals(设置))
的foreach(在child.ChildNodes XmlNode的节点)
如果(节点.Name.Equals(增加))
_ret.Add

node.Attributes [钥匙]。价值
node.Attributes [价值。价值
);
}
回报(_RET);
}



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


I have a c# .Net console app that performs FTP operations. Currently, I specify the settings in a custom configuration section, e.g.

<?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>

I would like to specify, in the command line, an external configuration file.

HOWEVER!!!...

I just realized that the above "FtpConfiguration" section doesn't truly belong in the application's app.config. My ultimate goal is that I will have many scheduled tasks that execute my console app like this:

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

Consequently, I believe I've gone down the wrong path and what I really want is something to read in my custom xml document but continue to use System.Configuration to get the values... as opposed to reading an XmlDocument and serializing it to get nodes/elements/attributes. (Although, I'm not against the latter if someone can show me some simple code)

Pointers would be greatly appreciated. Thanks.

Update: The answer I accepted was a link to another StackOverflow question, repeated here with my code - below which was exactly what I was looking for -- using the OpenMappedExeConfiguration to open my external config file

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"D:\Development\FileTransferHelper\Configuration\SampleInterface.config";

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

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

解决方案

If you want to use System.Configuration to open your custom files, you may want to check on this post: Loading custom configuration files. Oliver nails it in a very straightforward way.

Since you want to read parameters passed to your application via the command line, you may want to pay a visit to this MSDN post: Command Line Parameters Tutorial.

If you'd rather use a custom approach, there's a few ways you can accomplish this. One possibility is to implement a loader class, and consume your custom configuration files.

For example, let's assume a simple config file that looks like this:

spec1.config

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

A very simple, hashtable-like (key-value pair) structure.

An implemented parser/reader would then look something like this:

        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);
        }

Meanwhile, you'll still be able to use ConfigurationManager.AppSettings[] to read from the original app.config file.

这篇关于读取外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆