读取外部配置文件
我有一个执行FTP操作的ac。.Net控制台应用程序。 目前,我在自定义配置部分中指定设置,例如
我想在命令行中指定一个外部配置文件。
然而!!!…
我刚刚意识到上面的“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打开自定义文件,可能需要查看以下post: 加载自定义配置文件 。 奥利弗以非常直接的方式钉它。
由于您希望通过命令行读取传递给应用程序的参数,因此您可能需要访问此MSDNpost: 命令行参数教程 。
如果您更愿意使用自定义方法,可以采用几种方法来实现此目的。 一种可能性是实现一个loader类,并使用您的自定义配置文件。
例如,让我们假设一个简单的配置文件,如下所示:
spec1.config
一种非常简单的哈希表(键值对)结构。
实现的解析器/阅读器看起来像这样:
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
文件中读取。
如果你沿着自定义路径走,我老实说只使用JSON来存储配置然后反序列化以加载它并序列化来编写它。 Json.NET允许您非常轻松地执行此操作。
您的XML:
在JSON中看起来像这样:
{ "FtpConfiguration": { "Environment": { "Name": "QA", "SourceServer": { "HostName": "QA_hostname", "UserName": "QA_username", "Password": "QA_password", "Port": "21", "RemoteDirectory": "QA_remoteDirectory" }, "TargetServer": { "DownloadDirectory": "QA_downloadDirectory" } } } }
你的课程看起来像:
class Config { public FtpConfiguration FtpConfiguration { get; set; } } class FtpConfiguration { public Environment Environment { get; set; } } class Environment { public SourceServer SourceServer { get; set; } public TargetServer TargetServer { get; set; } } class SourceServer { public string HostName { get; set; } public string UserName { get; set; } public string Password { get; set; } public int Port { get; set; } public string RemoteDirectory { get; set; } } class TargetServer { public string DownloadDirectory { get; set; } }
您可以将设置保存到这样的对象:
var config = new Config() { FtpConfiguration = new FtpConfiguration() { Environment = new Environment() { SourceServer = new SourceServer() { HostName = "localhost", UserName = "jaxrtech", Password = "stackoverflowiscool", Port = 9090, RemoteDirectory = "/data", }, TargetServer = new TargetServer() { DownloadDirectory = "/downloads" } } } };
然后你可以像这样写入文件(如果它是一个更大的文件,则使用Stream
):
string json = JsonConvert.SerializeObject(config); File.WriteAllText("config.json", json);
然后你可以像这样读取文件(再次你可以使用Stream
):
string json = File.ReadAllText("config.json"); Config config = JsonConvert.DeserializeObject(json);
我首选的解决方案使用XDocument。 我没有测试过,所以可能会有一些小问题,但这是为了certificate我的观点。
public Dictionary GetSettings(string path) { var document = XDocument.Load(path); var root = document.Root; var results = root .Elements() .ToDictionary(element => element.Name.ToString(), element => element.Value); return results; }
将返回一个字典,其中包含来自表单的xml的元素名称和值:
QA_hostname QA_username
我发现这个解决方案很好,因为它的整体简洁。
同样,我不希望这完全按原样工作。 使用XAttributes和XElements等,你绝对可以使它更像你的原始。 过滤很容易。