如果標題不明確:我想讀取並寫入某些特定的.xml
配置文件。 System.Configuration
可能嗎?我可以使用System.Configuration來處理特定的配置文件嗎?
回答
你可以很容易地:
/// <summary>
/// Gets any configuration file configuration file object.
/// </summary>
/// <param name="path">Path to configuration file.</param>
/// <param name="createIfNotExist">If true will create new configuration file if is doesn't exist.</param>
/// <returns>Configuration file object.</returns>
public static Configuration GetFileConfig(string path, bool createIfNotExist)
{
if (!File.Exists(path))
{
if (createIfNotExist)
{
using (XmlTextWriter xml = new XmlTextWriter(path, null))
{
xml.WriteStartDocument();
xml.WriteStartElement("configuration");
xml.WriteStartElement("appSettings");
xml.WriteEndElement();
xml.WriteStartElement("connectionStrings");
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteEndDocument();
}
}
else
{
throw new ArgumentException("Path doesn't exist", "path");
}
}
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = path };
return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
據我所知,這是不可能的。
名稱空間中的類僅在預期位置(應用程序文件夾,web.config文件和鏈接的配置文件)中識別.config
文件。
從.xml
將文件重命名爲.config
有這樣的麻煩嗎?
更新(以下注釋):
可以使用ConfigSource
在配置部分以指向一個單獨的文件:
<connectionStrings configSourc="myConnectionStrings.config" />
當然這是n OT。我只想將應用程序設置存儲在某個特定的預定義目錄中(讓它們成爲'.config'文件),我找不到指定該文件完整路徑的方式。 – zerkms 2011-05-22 06:16:06
@zerkms - 看看['ConfigSource'](http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx) - 這應該做你想做的。 – Oded 2011-05-22 06:17:17
@Oded:如果我現在沒有'app.config'文件(雖然它是WPF應用程序),並且該文件的路徑是「當前用戶的基於AppData目錄的路徑」,該怎麼辦? – zerkms 2011-05-22 06:20:00
- 1. 我可以配置cxf使用特定的XML解析器嗎?
- 2. 我可以使用getopt按特定順序處理選項嗎?
- 3. 我可以使用Editbox的OnChange事件來處理線程嗎?
- 4. 我可以使用屬性文件和運行時配置來配置log4cxx嗎?
- 5. System.Configuration配置管理器
- 6. 我可以使用服務來處理觸摸事件嗎?
- 7. Maven - 我可以在配置文件定義中引用配置文件ID嗎?
- 8. 您可以指定Arquillian使用特定的Wildfly配置嗎?
- 9. 你可以使用make來處理命令行指定的文件嗎?
- 10. (How)我可以爲我的nhibernate配置指定一個特定的配置文件名嗎?
- 11. 我可以使用requirements.txt文件來處理本地python目錄嗎?
- 12. 我可以使用grep來查找包含特定字符串的文件嗎?
- 13. 可以使用p2來管理Eclipse RCP應用程序的可自定義漫遊配置文件嗎?
- 14. 我可以指定一個默認的AWS配置文件嗎?
- 15. 我可以使用msbuild更新非xml配置文件嗎?
- 16. 我可以使用配置文件作爲鍵值對嗎?
- 17. 我可以使用配置文件來保存連接字符串參數嗎?
- 18. 我可以使用Zend_Log來處理PHP錯誤嗎?
- 19. 我可以使用C++來處理Twitter API嗎?
- 20. 我可以使用.load()JQuery函數來處理表單嗎?
- 21. 我可以使用遞歸來處理GPIO嗎?
- 22. 我可以使用閃存來處理視頻嗎?
- 23. 我們可以配置TFS只接受來自特定分支的合併嗎?
- 24. 在PHPUnderControl中,我可以配置PHP_CodeSniffer來忽略特定的目錄嗎?
- 25. 使用特定的Automapper配置文件
- 26. 我可以在特定像素處放置元素嗎?
- 27. 使用WIX處理env特定文件
- 28. high_voltage gem可以處理.haml.html文件嗎?
- 29. FDT可以處理.fla文件嗎?
- 30. 我可以使用我的ssh-public-key來解密文件嗎?
我沒有看到我在這裏指定了樣本中的任何**具體**路徑。 – zerkms 2011-05-22 06:24:52
更新時,路徑在ExeConfigurationFileMap的對象初始值設定項中。 – 2011-05-22 06:25:50