您可以找到App.Config中的应用程序之外的安装目录,并将其放置在一个共同的文件夹(如应用程序数据)。然后你会告诉你的应用程序从那里加载它,而不是从应用程序安装目录中取出它。
ConfigurationManager.OpenMappedExeConfig允许您从一个自定义的位置加载你的配置。
// Access a configuration file using mapping.
// This function uses the OpenMappedExeConfiguration
// method to access a new configuration file.
// It also gets the custom ConsoleSection and
// sets its ConsoleEment BackgroundColor and
// ForegroundColor properties to green and red
// respectively. Then it uses these properties to
// set the console colors.
public static void MapExeConfiguration()
{
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
Console.WriteLine(config.FilePath);
if (config == null)
{
Console.WriteLine(
"The configuration file does not exist.");
Console.WriteLine(
"Use OpenExeConfiguration to create the file.");
}
// Create a new configuration file by saving
// the application configuration to a new file.
string appName =
Environment.GetCommandLineArgs()[0];
string configFile = string.Concat(appName,
".2.config");
config.SaveAs(configFile, ConfigurationSaveMode.Full);
// Map the new configuration file.
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
// Get the mapped configuration file
config =
ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
// Make changes to the new configuration file.
// This is to show that this file is the
// one that is used.
string sectionName = "consoleSection";
ConsoleSection customSection =
(ConsoleSection)config.GetSection(sectionName);
if (customSection == null)
{
customSection = new ConsoleSection();
config.Sections.Add(sectionName, customSection);
}
else
// Change the section configuration values.
customSection =
(ConsoleSection)config.GetSection(sectionName);
customSection.ConsoleElement.BackgroundColor =
ConsoleColor.Green;
customSection.ConsoleElement.ForegroundColor =
ConsoleColor.Red;
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section. This
// makes the new values available for reading.
ConfigurationManager.RefreshSection(sectionName);
// Set console properties using the
// configuration values contained in the
// new configuration file.
Console.BackgroundColor =
customSection.ConsoleElement.BackgroundColor;
Console.ForegroundColor =
customSection.ConsoleElement.ForegroundColor;
Console.Clear();
Console.WriteLine();
Console.WriteLine("Using OpenMappedExeConfiguration.");
Console.WriteLine("Configuration file is: {0}",
config.FilePath);
}
样品来源:MSDN
特别是对于作为万能的'LocalSystem'帐户运行的服务,我会非常警惕具有通过非管理员可写设置。我认为,从安装和升级的角度来看,不要编辑存储在Program Files中的文件是一个很好的决定,但我建议在编辑应用程序中嵌入一个清单,以要求管理员权限运行它,并使其应用对文件,注册表项或任何用于存储的任何非常严格的ACL。 – shambulator 2013-02-22 23:41:22
有道理。你能否指出我关于嵌入清单和设置ACL的任何文档的方向? – jdehlin 2013-02-23 19:54:50