0

I have a C# solution (visual studio 2017) with two projects lets say A and B, both compiled to exe. Project A depends on project B's exe, I though a dependency to project B would solve this. However, the exe doesn't get copied automatically when build.

Does the dependencies not work for exe's ? Is the only solution to make an post build step ?

dennis_ler
  • 659
  • 1
  • 9
  • 36
  • 2
    Why are you building two exe's instead of putting the necessary functionality in a class library that you can reference from both apps? – mason Jun 07 '17 at 12:21
  • @mason maybe he wants two separate exe files because he might want to run then on demand individually? – pitersmx Jun 07 '17 at 12:23
  • @pitersmx AFAICT, mason's suggestion was to still have two exe files, but to have a third dll which both those exes reference. –  Jun 07 '17 at 12:25
  • @mason pretty simple, A will start B and this is needed to encapsulate a buggy 3'rd party dll, and this isn't solved using AppDomains. – dennis_ler Jun 07 '17 at 12:46

3 Answers3

2

Have a look at the Properties of your dependencies and set "Local Copy" for your exe, this should copy the exe to your A.exe's bin folder.

Edit: It's the german version, but should look like this:

Dependency Properies

The J
  • 41
  • 8
  • This is not a reference, it is a dependency in the solution between projects. What I'm trying to say is, that I can't find that property at solution level dependencies. – dennis_ler Jun 07 '17 at 12:44
  • added an image for clarifying :) – The J Jun 07 '17 at 13:01
  • Aha, well I didn't think about referencing the project I only had setup the dependency, this solves it. Thanks. – dennis_ler Jun 07 '17 at 13:06
2

I think you want a post-build event.

It sounds like you already have the Project Dependency set up, which is good, because it's important that Project B gets built before Project A. And adding a Project Dependency for Project B, to Project A, is the way to do that.

Here's the documentation for adding/removing Project Dependencies: https://msdn.microsoft.com/en-us/library/et61xzb3.aspx

In Project A you can add a post build event that will copy the Project B exe.

  1. Right Click Project A, and choose "Properties".
  2. Change to the "Build Events" tab.
  3. Add the command: copy "FromPath" "ToPath"

The documentation for the copy command is here: https://technet.microsoft.com/en-gb/library/bb490886.aspx?f=255&MSPPError=-2147217396

If you click "Edit Post-build", you'll get a mini-editor which has a button labelled "Macros".

If you click that "Macros" button then it'll allow you to insert replacement strings in the format $(foo).

These macros allow you to get things like the project or solution folder, so you can easily build paths that point to the right places.

Here's some documentation on the build events: https://learn.microsoft.com/en-us/visualstudio/ide/how-to-specify-build-events-csharp

Here's some (crappy) documentation on the Build Events properties page: https://learn.microsoft.com/en-us/visualstudio/ide/reference/build-events-page-project-designer-csharp

Here's some (useful) documentation on the macros: https://msdn.microsoft.com/en-us/library/c02as0cs.aspx

  • The solution with adding a reference to the project works, then I avoid the post build step. – dennis_ler Jun 07 '17 at 13:08
  • 2
    Adding it as a reference has other side effects though. The code from the one project will be accessible in the other project. That's probably not an issue, but it might get slightly confusing. Another issue is that the project now must be a .net project, and the .net framework versions and types must be compatible. So if Project A was .net 4.5, Project B couldn't be made using c++, nor could it be .net 4.6. Again, this is probably unlikely for your situation, but it's worth pointing out. –  Jun 07 '17 at 13:13
  • Yes, you are right, didn't think about the reduced decoupling between the projects. They are both same :net versions so not a problem, so I guess it must be solved post build eventhough I would like to avoid that. – dennis_ler Jun 07 '17 at 16:05
0

You can use the Post-build events ( Project properties -> Build Events ) to copy the file at the end of build.

JSR
  • 188
  • 8