c# – 读取外部配置文件

我有一个执行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:\Development\FileTransferHelper\Configuration\SampleInterface.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文件中读取.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...