2

I have a VS2010.NET solution with many projects. One of the projects has a non-source code directory in it. When I build this solution, I need to have that directory copied over to the solution build output directory. What I am trying to do is access the files in that folder at my solution runtime. Since this application will be distributed to multiple computers, I need to also distribute this set of data files.

To review, here's what my (simplified) solution hierarchy looks like

solution
|
+----MainProject
|    |
|    +----source.cs
|
+----SupportPorject
     |
     +----source.cs
     +----MyFolder
          |
          +----DataFile1
          +----DataFile2
          +----DataFile3

When I build the solution, I would like to have the following

solution
|
+----bin
     |
     +----Debug
     |    |
     |    +----Solution.exe
     |    +----SupportProject.dll
     |    +----SupportProject.pdb
     |    +----MyFolder
     |         |
     |         +----DataFile1
     |         +----DataFile2
     |         +----DataFile3
     |
     +----Release
          |
          +----Solution.exe
          +----SupportProject.dll
          +----MyFolder
               |
               +----DataFile1
               +----DataFile2
               +----DataFile3
Paul Grinberg
  • 1,184
  • 14
  • 37
  • perhaps i'm missing the nuance, but I think as long as you have `MyFolder` set to copy out, it should make it exactly as you describe. What are you getting currently? – crthompson Feb 17 '15 at 22:24
  • Why don't you make a simple copy command that runs in the post build event? – Beemen Feb 17 '15 at 22:42

4 Answers4

1

You may want to use Xcopy in your Visual Studio Build Pre/Post events depending on the sequence you need the files in. xcopy SourceFolder $(SolutionDir)YourFolderNameHere*.*" /E /H

As explained in this post over here

Community
  • 1
  • 1
Max Kimambo
  • 406
  • 4
  • 10
  • As I understand the XCOPY solution, it must be specified in the pre or post build event of my "support" project. If that's the case, then I see two issues: 1. I have to hard code the solution name into the XCOPY command. Is there some environment variable for that? 2. I have to hard code just one of debug or release output paths into the XCOPY command. Is there some environment variable for that? – Paul Grinberg Feb 18 '15 at 12:45
1

Another solution is edit yours .csproj and search for OutputPath tags. For Debug output, it must look similar to:

<OutputPath>bin\Debug\</OutputPath>

According to your requirements, set it to:

<OutputPath>..\bin\Debug\</OutputPath>
denys-vega
  • 3,522
  • 1
  • 19
  • 24
1

After a lot more research, I stumbled on the answer (in part based on this answer to a similar question). The solution involved hand modifying the SupportProject.cproj file with the following

<ItemGroup>
    <Content Include="MyFolder\**">
        <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>
Community
  • 1
  • 1
Paul Grinberg
  • 1,184
  • 14
  • 37
  • Should be marked as answer, it works. The link element is not required, cause it shows the content twice into the solution, you can mark your own answer as answer :) – rfcdejong Jun 18 '19 at 10:42
0

Visual Studio also has an option to copy files to the output directory. Just change your content from "Do Not Copy" to "Copy Always". You can find the setting in file properties.

gaiazov
  • 1,908
  • 14
  • 26
  • I know about the "copy always" option for files within a project. However, my example was very simplified. Rather than having just 3 files inside MyFolder, I actually have hundreds of files spread across multiple subfolders (and sub-subfolders), and their hierarchy must be preserved. When I click on my top level "MyFolder", I do not get an option to "always copy" – Paul Grinberg Feb 18 '15 at 12:49