c#在exe

时间:2016-07-06 14:15:00

标签: c# console-application app-config

我有一个控制台程序' A'在给定的点上将运行程序' B'和节目' C'。但是我的app.config与每个程序都存在问题。基本上程序A只是一个调用不同控制台应用程序的包装类,它不应该有任何app.config,但它应该使用当前正在运行的程序的app配置。因此理论上,程序B应该只有2个app.config,而程序C应该只有2个。

因此,如果我们运行程序A并且程序B被执行,它应该使用程序B的app.config来获取信息,在程序C执行之后它应该使用程序C的app.config 。

有办法做到这一点吗?目前我正在这样做:

var value =  ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["ProgramBKey"].Value;

它似乎不起作用。我检查了Assembly.GetExecutingAssembly().Location上的调试它的变量是\ bin \ 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>

4 个答案:

答案 0 :(得分:3)

编辑:以下答案与原帖中的这个问题有关,&#34;是否可以在程序的exe中编译B和C的app.config。&#34;

您可以使用&#34; Embedded Resource&#34;特征。以下是使用XML文件作为嵌入式资源的一个小例子:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{
  if([touch.view isKindOfClass: [self.view class]] == YES)
  {
    return YES; // return YES (the default) to allow the gesture recognizer to examine the touch object
  }
  else {
    return NO;  //NO to prevent the gesture recognizer from seeing this touch object.
  }
}

答案 1 :(得分:1)

对我来说,整个事情听起来像是一个&#34;设计问题&#34;。为什么要使用Programm A的配置打开Programm B?

您是所有这些程序的作者吗?您可能希望使用dll文件。这将节省您的麻烦,因为所有代码都运行程序的配置运行。

答案 2 :(得分:1)

您可以使用相同的配置文件拥有多个应用程序。这样,当您切换应用程序时,他们都可以找到自己的配置文件部分。

我通常这样做的方法是......首先让每个应用程序“做自己的事情”,然后将配置文件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>

答案 3 :(得分:0)

您可以这样做:

  • 在属性/构建操作中将 App.config 设为“嵌入式资源”

  • 复制到输出目录:不要复制

  • 将此代码添加到 Proram.cs Main

    UPDATE yourTable t1
    SET name = (SELECT name FROM yourTable t2
                WHERE t2.id_product = t1.id_product AND t2.id_lang = 2)
    WHERE
        name IS NULL;
    

以下是所需的功能:

      if (!File.Exists(Application.ExecutablePath + ".config"))
      {
          File.WriteAllBytes(Application.ExecutablePath + ".config", ResourceReadAllBytes("App.config"));
          Process.Start(Application.ExecutablePath);
          return;
      }