我有一个控制台程序“A”,在给定点将运行程序“B”和程序“C”。但是,我遇到了与每个程序关联的 app.config 问题。基本上程序A只是一个调用不同控制台应用程序的包装类,它不应该有任何app.config,但它应该使用当前运行程序的应用程序配置。所以理论上应该只有2个app.config,一个用于程序B,另一个用于程序C。
因此,如果我们运行程序A并且程序B被执行,它应该使用程序B的app.config来获取信息,并且在程序C执行后它应该使用程序C的app.config。
有办法做到这一点吗?目前我正在这样做:
var value = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["ProgramBKey"].Value;
这似乎不起作用。我检查了
Assembly.GetExecutingAssembly().Location
上的调试,它的变量是 in\Debug\ProgramB.exe 和 'ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings' 当有关键值时设置为 Count=0下面。
示例代码程序A:
static void Main(string[] args)
{
if(caseB)
B.Program.Main(args)
else if(caseC)
C.Program.Main(args)
}
程序 B 的示例 app.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ProgramBKey" value="Works" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
编辑:以下答案与原始帖子中的这个问题有关,“是否可以在程序的 exe 中编译 B 和 C 的 app.config。”
您可以使用“嵌入式资源”功能。以下是使用作为嵌入资源包含的 XML 文件的小示例:
public static class Config
{
static Config()
{
var doc = new XmlDocument();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Fully.Qualified.Name.Config.xml"))
{
if (stream == null)
{
throw new EndOfStreamException("Failed to read Fully.Qualified.Name.Config.xml from the assembly's embedded resources.");
}
using (var reader = new StreamReader(stream))
{
doc.LoadXml(reader.ReadToEnd());
}
}
XmlElement aValue = null;
XmlElement anotherValue = null;
var config = doc["config"];
if (config != null)
{
aValue = config["a-value"];
anotherValue = config["another-value"];
}
if (aValue == null || anotheValue == null)
{
throw new XmlException("Failed to parse Config.xml XmlDocument.");
}
AValueProperty = aValue.InnerText;
AnotherValueProperty = anotherValue.InnerText;
}
}
您可以使用同一个配置文件拥有多个应用程序。这样,当您切换应用程序时,它们都可以找到自己的配置文件部分。
我通常的做法是...首先让每个应用程序“做自己的事情”,然后将配置文件A的相关部分复制到配置文件B中。
它看起来像这样:
<configSections>
<sectionGroup>
<sectionGroup name="applicationSettings"...A>
<sectionGroup name="userSettings"...A>
<sectionGroup name="applicationSettings"...B>
<sectionGroup name="userSettings"...B>
<applicationSettings>
<A.Properties.Settings>
<B.Properties.Settings>
<userSettings>
<A.Properties.Settings>
<B.Properties.Settings>
对我来说,整件事听起来像是一个“设计问题”。为什么要使用程序 A 的配置来打开程序 B?
您是所有这些程序的作者吗?您可能想改用 dll 文件。这将为您省去麻烦,因为所有代码都在运行程序的配置下运行。
具体操作方法如下:
在属性/构建操作中将 App.config 设置为“嵌入式资源”
复制到输出目录:不要复制
将此代码添加到 Program.cs Main
if (!File.Exists(Application.ExecutablePath + ".config"))
{
File.WriteAllBytes(Application.ExecutablePath + ".config", ResourceReadAllBytes("App.config"));
Process.Start(Application.ExecutablePath);
return;
}
以下是需要的功能:
public static Stream GetResourceStream(string resName)
{
var currentAssembly = Assembly.GetExecutingAssembly();
return currentAssembly.GetManifestResourceStream(currentAssembly.GetName().Name + "." + resName);
}
public static byte[] ResourceReadAllBytes(string resourceName)
{
var file = GetResourceStream(resourceName);
byte[] all;
using (var reader = new BinaryReader(file))
{
all = reader.ReadBytes((int)file.Length);
}
file.Dispose();
return all;
}