.NET配置/ ConfigurationManager exePath

时间:2010-02-09 00:48:46

标签: c# .net configuration

创建Configuration对象时,它会询问配置文件的路径...我一直在为无效的文件路径抛出异常......什么是有效路径?我试过“config.config”和“。\ config.config”,但我想不出别的什么。

2 个答案:

答案 0 :(得分:2)

如果这是为了访问您的app.config文件,您可以使用System.Configuration.ConfigurationManager静态类,即

var setting = System.Configuration.ConfigurationManager.AppSettings["MySetting"];

修改:您可能需要这样的内容:

var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // choose ConfigurationUserLevel value, or provide @"c:\path\to\some\exe\"
configuration.AppSettings.Settings.Add("test", "value"); // for new configs
configuration.AppSettings.Settings["test2"].Value = "somevalue"; // for modification of existing keys
c.Save(); // or choose save location

答案 1 :(得分:0)

路径将位于.EXE所在的Environment.CurrentDirectory中。如果我没有弄错,app.config文件在添加到项目中时,当项目生成时,它将生成一个配置文件与.EXE编译到的位置相同,通常为bin\debugbin\release,例如,假设应用程序名称为foo.exe,其相关配置文件为{ {1}}。

希望这有帮助, 最好的祝福, 汤姆。