简体   繁体   English

获取引用项目的可执行文件名称

[英]Get the executable name of a referenced project

I have a solution with two projects, a master and a slave. 我有两个项目的解决方案,一个主项目和一个从项目。 Both these projects are WinForms Applications. 这两个项目都是WinForms应用程序。 There's always one instance of the master running and multiple instances of the slaves. 主节点始终有一个实例在运行,而从属实例则有多个实例。 The slaves are started by the master. 从站由主站启动。 Right now I have the executable name of the slave hardcoded into the master's code, which works fine. 现在,我已经将奴隶的可执行名称硬编码到主人的代码中,可以正常工作。 However, I want to be able to get the executable name of the slave without hardcoding it, but by getting it through its reference. 但是,我希望能够在不对其进行硬编码的情况下获得从属的可执行文件名称,而是通过其引用获取它。 Is this possible, and how should I go about doing this? 这可能吗,我应该怎么做呢?

This is what I currently have: 这是我目前拥有的:

ProcessStartInfo startInfo = new ProcessStartInfo("Slave.exe") { Arguments = Args };
Process.Start(startInfo);

But I would like to replace "Slave.exe" with something dynamical. 但是我想用动态的东西代替"Slave.exe" I have added an assembly reference to the slave project, having namespace Slave . 我已经为从项目添加了程序集引用,并命名空间为Slave

Thanks to @Postlagerkarte's suggestion, I solved my problem by using post build events. 感谢@Postlagerkarte的建议,我通过使用后期构建事件解决了我的问题。 I have 4 different slave processes, let's call them SlaveProcessA to SlaveProcessD . 我有4个不同的从属进程,我们将它们SlaveProcessASlaveProcessD First thing I did was make a class which can hold the executable names, and (de)serialize them: 我要做的第一件事是创建一个可以保存可执行文件名称的类,并对其进行反序列化:

public class ExeNames {
    [XmlIgnore]
    public bool IsLoaded {get; private set;}
    [XmlIgnore]
    const string Filename = "exenames.xml";
    public string SlaveAExe {get; set;}
    public string SlaveBExe {get; set;}
    public string SlaveDExe {get; set;}
    public string SlaveCExe {get; set;}

    public void Load() {
      var serializer = new XmlSerializer(typeof(ExeNames));
      using (StreamReader reader = new StreamReader(Filename)) {
        ExeNames End = serializer.Deserialize(reader) as ExeNames;
        if (End == null) return;
        SlaveAExe = End.SlaveAExe;
        SlaveBExe = End.SlaveBExe;
        SlaveDExe = End.SlaveDExe;
        SlaveCExe = End.SlaveCExe;
        IsLoaded = true;
      }
    }

    public void Save() {
      var serializer = new XmlSerializer(typeof(ExeNames));
      using (StreamWriter writer = new StreamWriter(Filename)) {
        serializer.Serialize(writer, this);
      }
    }
  }

After that, I created a new project called ExeNameSaver . 之后,我创建了一个名为ExeNameSaver的新项目。 This program takes 2 parameters: the code of the process, and the executable name: 该程序具有两个参数:进程的代码和可执行文件的名称:

namespace ExeNameSaver {
  class Program {
    static void Main(string[] args) {
      if (args.Length < 2) throw new ArgumentException("2 Parameters are required");
      var Code = args[0];
      var Exe = args[1];
      ExeNames.ExeNames Exn = new ExeNames.ExeNames();
      try {
        Exn.Load();
      }
      catch {
        Console.WriteLine("Starting new file...");
      }
      switch (Code) {
        case "SlaveA":
          Exn.SlaveAExe = Exe;
          break;
        case "SlaveB":
          Exn.SlaveBExe = Exe;
          break;
        case "SlaveC":
          Exn.SlaveCExe = Exe;
          break;
        case "SlaveD":
          Exn.SlaveDExe = Exe;
          break;
        default:
          Console.WriteLine("Invalid process code: " + Code);
          break;
      }
      Exn.Save();
    }
  }
}

Then, I make sure that the ExeNameSaver is built first by altering the dependencies. 然后,我通过更改依赖关系来确保首先构建ExeNameSaver。 Now I can use the ExeNameSaver program to save my executable names to an XML-file, by putting the following in the Post-build event command line of SlaveProcessA: 现在,通过将以下内容放入SlaveProcessA的Post-build事件命令行中,可以使用ExeNameSaver程序将可执行文件的名称保存到XML文件中:

$(OutDir)ExeNameSaver.exe SlaveA $(TargetFileName)

And similarly for the other 3 slaves. 对于其他3个奴隶也是如此。

$(OutDir)ExeNameSaver.exe SlaveB $(TargetFileName)

... ...

$(OutDir)ExeNameSaver.exe SlaveC $(TargetFileName)

... ...

$(OutDir)ExeNameSaver.exe SlaveD $(TargetFileName)

Now after each build, the exenames.xml file will contain the executable names of my 4 processes. 现在,在每次构建之后, exenames.xml文件将包含我的4个进程的可执行文件名称。 I can then load this XML using the same ExeNames class in my master program and use their names from there. 然后,我可以在主程序中使用相同的ExeNames类加载此XML,并从那里使用它们的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取引用项目的程序集名称 - Get assembly name of referenced project 从通过代码引用dll的位置获取项目的名称 - Get name of project from where dll is referenced via code 在C#中,如何从引用的项目中获取调用项目的名称? - In C#, how can I get the name of the calling project from within a referenced project? 引用的项目是一个非独立的可执行文件。 自包含的可执行文件不能引用非自包含的可执行文件 - The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable xaml引用的依赖项不是从项目文件夹中加载的,而是从可执行文件文件夹中加载的 - Dependencies referenced by xaml are not loaded from project folder but from executable folder 获取引用方法的程序集名称 - Get assembly name of referenced method 在引用的项目中获取主项目的版本 - Get main project's version in a referenced project 如何从引用的dll获取可执行文件的汇编信息? - How to get assembly information of executable from the referenced dll? 如何从VisualSstudio中的引用项目获取资源 - How to get resources from a referenced project in VisualSstudio 在单元测试期间获取引用项目的路径 - Get path of referenced project during unit testing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM