Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding files to the bin directory at Build and Publish

I have two license files that I would like to include in my \bin directory both when I build and publish.

Both files are in the App_Data directory (their initial location doesn't matter, they just need to end up in the \bin) and have the following properties set:

  • Build Action = Content
  • Copy to Output Directory = Copy Always

They are in not the \bin when I build or publish.

What is wrong with my setup: the settings, the folders, the files, something else...?

UPDATE

I moved the files out of the App_Data directory and placed them in the project root and now they are copied to the \bin on build.

like image 387
wilsjd Avatar asked Aug 07 '13 18:08

wilsjd


2 Answers

I've done this in a few projects by expanding my .csproject file slightly. The following code should be put directly beneath the Project node in your WebProject.csproj. The AfterBuild target simply copies a set of files ("unreferenced DLLs" in this case) to the bin-folder when building normally from Visual Studio. The CustomCollectFiles basically do the same thing when deploying.

  <PropertyGroup>
    <UnreferencedDlls>..\lib\Unreferenced\**\*.dll</UnreferencedDlls>
  </PropertyGroup>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>
  </PropertyGroup>
  <Target Name="AfterBuild">
    <Message Text="Copying unreferenced DLLs to bin" Importance="High" />
    <CreateItem Include="$(UnreferencedDlls)">
      <Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
    </CreateItem>
    <Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="bin\%(RecursiveDir)" SkipUnchangedFiles="true" />
  </Target>
  <Target Name="CustomCollectFiles">
    <Message Text="Publishing unreferenced DLLs" Importance="High" />
    <ItemGroup>
      <_CustomFiles Include="$(UnreferencedDlls)" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

The part you need to modify is basically the UnreferencedDlls node to match your folder structure. The **\*.dll part simply means "every DLL file at any level beneath here".

like image 106
Arve Systad Avatar answered Oct 13 '22 23:10

Arve Systad


If you're using Visual Studio:

  • Show your file properties (Click on your file or Right-click on it then choose Properties)
  • At the Copy to Output Directory property choose Copy always or Copy if newer.

At build time, the file is going to be copied at the bin directory: Debug or Release...

like image 26
Kreshnik Avatar answered Oct 13 '22 22:10

Kreshnik