Search code examples
c#visual-studiobuildmsbuildbuild-process

Run a c# console application in build process


Suppose we have a solution with three projects:

enter image description here

  1. DownloadDataBase is a console application that downloads data from web and creates a sqlite db file
  2. Test is the main windows from application that uses the downloaded sqlite database.
  3. TestLibrary is a class library that is referenced by the Test project.

I know How I can reorder the build of these three projects using Configuration Manager from build menu. But The main question is : How can I customize build process of the solution so that:

  • It first builds the DownloadDataBase
  • Then executes this console application DownloadDataBase
  • After that copy the created sqlite file from the last step to the resource directory of the Test Project
  • And Finally builds the Test project using the updated sqlite file?

Therefore every time that I build the new release I will have the latest data needed by my application.

Thanks for any help


Solution

  • You'll need to customize your MSBuild project files (i.e. .csproj files).

    For example, in your DownloadDatabase project, add at the bottom of the XML file, inside the <project> element:

    <Target AfterTargets="AfterBuild">
        <Exec Command="$(ProjectDir)$(OutputPath)$(AssemblyName).$(OutputType)" />
        <Copy SourceFiles="$(ProjectDir)$(OutputPath)file.db" DestinationFolder="$(SolutionDir)Test" />
    </Target>
    

    Maybe there's some mistake, but at the end of the day, you need to use MSBuild tasks to perform these actions.

    See Exec and Copy MSBuild task documentation pages to get further details.