如何更改配置文件的文件扩展名(c#)
答
像@Dave一样,我假设你是指应用程序配置文件。在这种情况下“不容易”。你可以做到这一点,但它涉及产生第二0123'与定制AppDomainSetup.ConfigurationFile
。说实话,我不认为这是值得的努力,但下面的作品(第二次打印"bar"
) - 但首先警告:这将是一个痛苦,特别是像解决方案的路径,证据等直接的东西。我真的不打扰...
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ConfigurationManager.AppSettings["foo"]);
if (!args.Contains("run"))
{
AppDomainSetup setup = new AppDomainSetup();
setup.ConfigurationFile = "foo.xml";
AppDomain dmn = AppDomain.CreateDomain("Foo", null, setup);
dmn.ExecuteAssembly(Assembly.GetEntryAssembly().CodeBase,
new string[] { "run" } /* crude exit condition */
);
}
}
}
与配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
</configuration>
答
您还可以创建自定义栏目几乎是空的配置文件。然后使用自定义节的configSource属性来指定外部配置文件。这个外部文件可以有你想要的任何扩展名。从.exe.config文件与
提取物:
<configuration>
<configSections>
<section name="mappings" type="MappingsSection, [AssemblyName]"/>
<configSections>
<mappings configSource="Mappings.conf" />
从mappings.conf提取物:
<?xml version="1.0" encoding="utf-8" ?>
<mappings>
...
</mappings>
要创建自定义栏目,您可以使用this page
你指的是your_appname.exe.config? – Dave 2010-09-08 03:53:27