Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 .NETCore class library copy folder to bin/Debug/net452

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

like image 443
jmc Avatar asked Oct 21 '16 07:10

jmc


People also ask

Where is Copy to output directory property?

Add the file to your project, then in the file properties select under "Copy to Output Directory" either "Copy Always" or "Copy if Newer".

How do I Copy a folder in Visual Studio?

Use the CopyDirectory method to copy a directory to another directory. This method copies the contents of the directory as well as the directory itself. If the target directory does not exist, it will be created.

What is bin debug folder Visual Studio?

The bin folder holds binary files, which are the actual executable code for your application or library. Each of these folders are further subdivided into Debug and Release folders, which simply correspond to the project's build configurations.

How to create a class library project in Visual Studio?

In the Name text box, enter "StringLibrary" as the name of the project. Select OK to create the class library project. The code window then opens in the Visual Studio development environment.

What is copy to output directory in Visual Studio?

Copy to Output Directory is a property for files within a Visual Studio project. Select a Windows Form in a project, find Copy to Output Directory and note Copy to Output Directory is set to [Do not copy].

What are the debug and release build configurations in Visual Studio?

When you use a Visual Studio project template to create an app, Visual Studio automatically creates required settings for Debug and Release build configurations. You can change these settings if necessary. For more information, see the following articles: For the debugger to attach to a C++ DLL, the C++ code must emit DebuggableAttribute.

How to copy files to the build folder of a project?

For files, residing outside of the project folder working with Post build events provides the capability to copy files to the build folder of a project. Select project properties. Select the Compile tab for VB.NET, Build Events for C#.. In the bottom right hand corner select the Build Events... button (VB.ET only).


1 Answers

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();
}
like image 153
gilmishal Avatar answered Oct 19 '22 17:10

gilmishal