6

I have a .NET Core class library project ClassLibrary1 created in Visual Studio 2015. In this class library project, I added a folder NewFolder that will be used by whichever project is referencing ClassLibrary1.

So for instance, I have a project called ConsoleApplication1 that references ClassLibrary1. Every time I build ConsoleApplication1, NewFolder should be copied

FROM

PROJECT_PATH\ClassLibrary1\bin\Debug\net452\win7-x64\NewFolder

TO

PROJECT_PATH\ConsoleApplication1\bin\Debug\net452\win7-x64\NewFolder

UPDATE:

I added the following to the project.json file of my ClassLibrary1 project.

"buildOptions: {
    "copyToOuput": "NewFolder"
}"

It only copies the the folder to PROJECT_PATH\ClassLibrary1\bin\Debug\net452\win7-x64\NewFolder but not to PROJECT_PATH\ConsoleApplication1\bin\Debug\net452\win7-x64\NewFolder every time I build ConsoleApplication1.

I have no idea how this should be done in .NET Core projects because this does not seem to be the behaviour for non .NET Core projects.

UPDATE 2:

In non .NET Core projects I simply add a script to the post-build events command line of ClassLibrary1 and it automagically copies everything in the ClassLibrary1/bin/Debug including files and folders to ConsoleApplication1/bin/Debug.

xcopy "$(ProjectDir)NewFolder" "$(TargetDir)NewFolder" /E /Y

jmc
  • 1,649
  • 6
  • 26
  • 47

1 Answers1

1

What I usually do in non .NET Core is create a link to the folder/item in the project (your case ConsoleApplication1 and then set it to be copied to output.

any way, this can't be done in .net core.

What you are doing is also wrong, the project.json of ClassLibrary1 does not copy files to the output of ConsoleApplication1 what you should do is add the following to your project.json of ConsoleApplication1

"copyToOutput": {

  "mappings": {
    "NewFolder/": {
      "include": "../ClassLibrary1/bin/Debug/net452/win7-x64/NewFolder/**"
    }
  }

}

mappings - defines the folders to copy to (NewFolder) (the / is apparently necessary)

include - Defines glob patterns and file paths to include for copying to build output.

UPDATE

you can embed your files using buildOptions and accessing it using

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}
Community
  • 1
  • 1
gilmishal
  • 1,884
  • 1
  • 22
  • 37
  • this works! however, it requires that I put the configuration in `ConsoleApplication1`. I was actually looking for a solution where the configuration is only written in `ClassLibrary1` like the behaviour in non .NET Core projects. Please see `UPDATE 2`. Im not sure if it is possible though – jmc Oct 25 '16 at 06:23
  • Looking at this https://github.com/dotnet/cli/issues/2902 issue, there is a bug with `copyToOutput` that won't be fixed soon. The last comment suggests embedding your files in the dll, and accessing it accordingly – gilmishal Oct 26 '16 at 09:34