我有一個執行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");
正如注意,如果你決定只解析XML:XmlDocument的是在C#中處理XML的老路上,而你肯定能做到這一點,我建議使用的XDocument ,pa Linq-to-Xml的rt。 – Magus