Ray*_*ond 16 c# configuration system.configuration
我有一个执行FTP操作的ac..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>
Run Code Online (Sandbox Code Playgroud)
我想在命令行中指定一个外部配置文件.
然而!!!...
我刚刚意识到上面的"FtpConfiguration"部分并不真正属于应用程序的app.config.我的最终目标是,我将有许多执行我的控制台应用程序的计划任务,如下所示:
FileTransferHelper.exe -c FtpApplication1.config
FileTransferHelper.exe -c FtpApplication2.config
...
FileTransferHelper.exe -c FtpApplication99.config
Run Code Online (Sandbox Code Playgroud)
因此,我相信我走错了路,我真正想要的是在我的自定义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");
Run Code Online (Sandbox Code Playgroud)
Ono*_*dai 17
如果要使用System.Configuration打开自定义文件,可能需要查看以下帖子:加载自定义配置文件.奥利弗以非常直接的方式钉它.
由于您希望通过命令行读取传递给应用程序的参数,因此您可能需要访问此MSDN帖子:命令行参数教程.
如果您更愿意使用自定义方法,可以通过几种方法来实现此目的.一种可能性是实现一个loader类,并使用您的自定义配置文件.
例如,让我们假设一个简单的配置文件,如下所示:
spec1.config
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<add key="hostname" value="QA_hostname" />
<add key="username" value="QA_username" />
</Settings>
Run Code Online (Sandbox Code Playgroud)
一种非常简单的哈希表(键值对)结构.
实现的解析器/阅读器看起来像这样:
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);
}
Run Code Online (Sandbox Code Playgroud)
同时,您仍然可以使用ConfigurationManager.AppSettings[]原始app.config文件进行读取.
Jos*_*den 12
如果你沿着自定义路径走下去,老实说,我只是使用JSON来存储配置,然后反序列化以加载它并序列化来编写它.Json.NET允许您非常轻松地完成此操作.
您的XML:
<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>
Run Code Online (Sandbox Code Playgroud)
在JSON中看起来像这样:
{
"FtpConfiguration": {
"Environment": {
"Name": "QA",
"SourceServer": {
"HostName": "QA_hostname",
"UserName": "QA_username",
"Password": "QA_password",
"Port": "21",
"RemoteDirectory": "QA_remoteDirectory"
},
"TargetServer": {
"DownloadDirectory": "QA_downloadDirectory"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的课程看起来像:
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; }
}
Run Code Online (Sandbox Code Playgroud)
您可以将设置保存到这样的对象:
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"
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样写入文件(或使用Streamif如果它是一个更大的文件):
string json = JsonConvert.SerializeObject(config);
File.WriteAllText("config.json", json);
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样读取文件(再次你可以用一个Stream代替):
string json = File.ReadAllText("config.json");
Config config = JsonConvert.DeserializeObject<Config>(json);
Run Code Online (Sandbox Code Playgroud)
我首选的解决方案使用XDocument.我没有测试过,所以可能会有一些小问题,但这是为了证明我的观点.
public Dictionary<string, string> 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;
}
Run Code Online (Sandbox Code Playgroud)
将返回一个字典,其中包含来自表单的xml的元素名称和值:
<?xml version="1.0" encoding="utf-8"?>
<root>
<hostname>QA_hostname</hostname>
<username>QA_username</username>
</root>
Run Code Online (Sandbox Code Playgroud)
我发现这个解决方案很好,因为它的整体简洁.
同样,我不希望这完全按原样工作.使用XAttributes和XElements等,你绝对可以使它更像你的原始.过滤很容易.