In my C# project I have some .docx,.xlsx,*.jpg files in an folder, I want to make all these files as Content and while Setup and Deployment Project I want to add all these files as Content Output for That I have done follwing steps:
Unload Project from property window and then selected to Edit .csproj file.
Added below lines in <ItemGroup> to make all files in folder as Content and linked them to a folder
<Content Include="Lib\MyApp\Support\**\*.*">
<Link>Support\%(RecursiveDir)%(FileName).%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Lib\MyApp\Files\**\*.*">
<Link>Files\%(RecursiveDir)%(FileName).%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Reload Project
After reloading project I got lots of warning like
Warning 34 The file 'Lib\MyApp\Files\abc.bmp' could not be added to
the project. Cannot add a link to the file
D:\MyApp\Lib\MyApp\Files\abc.bmp. This file is within the project
directory tree.
I have Excluded the Lib folder from project, Why I am getting such warnings what I am doing wrong? Is it safe to ignore such warnings.
Update
After modifying csproj file to this
<Content Include="Lib\QOES\Support\**\*.*">
<Link>Support\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="Lib\QOES\Files\**\*.*">
<Link>Files\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
after adding <Visible>false</Visible> removed warnings, but is this correct way?
Adding <Visible>false</Visible> seems to be a very convenient solution to get rid of the Visual Studio warning:
The file <filename> could not be added to the project. This file is
within the project directory tree.
According to: MSDN: The file 'file' could not be added to the project, one of the reasons is relative paths being ambiguous. Meaning you have the file path Lib/MyApp/Files/abc.bmp both as a file path in your project folder as well as on your D: drive leading to ambiguity. That is my guess. Adding files by hand to the project folder can lead to issues.
In my case where I wanted to place a native binary aside of the executable, using <TargetPath> instead of <Link> worked:
<Content Include="Lib\somelibrary.dll">
<TargetPath>somelibrary.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Related
I've developed a custom NuGet which uses DotNetProjects.DotNetSiemensPLCToolBoxLibrary. DotNet library includes external dlls that are copied into the output directory following these statements:
<Content Include="..\externalDlls\libnodave_jfkmod.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\externalDlls\libnodave_jfkmod64.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
However, when my custom Nuget is used in another project, DotNet dlls are shown on project tree but not copied into the output directory.
nuget use example
If I set manually that these dlls will always be copied into output directory it works. It is posible to do so in the .csproj of my custom nuget?.
You can modify the .nuspec file instead of csproj file.
<files>
<!-- the dlls are copied from src to target-->
<file src="lib\**" target="lib/{framework name}[{version}]" />
</files>
The link: Support multiple .NET versions
I am trying to create a NuGet package that can contain and deploy third party dependencies, these are dll's and executables that my program needs to run. There are a few caveats to this issues though.
When installing the package the dlls need to be in one directory above the bin folder, I can already make them install into the bin folder thats not what I need. (This makes this question different than others I have found)
I would prefer using the packing via the .csproj file instead of the .nuspec if possible.
If I can't place them one level up I could probably make this work installing them into a folder within the bin folder. These requirements are a little weird but I'm a bound by work that has already been done and don't have power to change it.
A small example of one of the many things I have tried:
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<copyToOutput>true</copyToOutput>
<PackagePath>lib</PackagePath>
<PackageOutputPath>..\SQLite</PackageOutputPath>
<Pack>true</Pack>
</Content>
Use this:
<ItemGroup>
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<copyToOutput>true</copyToOutput>
<PackagePath>lib\$(targetframework)</PackagePath>
<Pack>true</Pack>
</Content>
</ItemGroup>
<PropertyGroup>
<PackageOutputPath>..\SQLite</PackageOutputPath>
</PropertyGroup>
Then, re-pack your project. Before you install this new version, please delete all old nuget caches under C:\Users\xxx\.nuget\packages, then reinstall the new version. After that, re-build your main project and then you will get what you want.
Update
Try to pack the files into content node:
<ItemGroup>
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
Then, right-click on the Project-->Pack. And you should not use any other nuspec file to pack with.
I created a class library project and put my SQLite database in it, and set these lines in the .csproj file:
<ItemGroup>
<Content Include="Data\DataBase\locations.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If I locally reference that class library to my main project (ASP.NET Core), it works fine and on every build it will copy the database file to this directory:
***\bin\Debug\net5.0\Data\DataBase
But when I publish the class library to NuGet, and install it in my main project, it doesn't copy the file.
Any help why this is happening and what should I do?
This is my package in NuGet package explorer
Just find out the answer
<ItemGroup>
<Content Include="Data\DataBase\locations.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
This line will do the job
<PackageCopyToOutput>true</PackageCopyToOutput>
I am using Visual Studio 2019 and creating NuGet packages successfully with this method:
https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli
All going well, but there are some settings (.json) files contained within a directory PageSettings/
When I publish my NuGet package and then install it into a new project, this directory appears in VS as a linked item (see pic). So as a user I can access the files, but they don't "exist" when the project is run.
This means if I run this project without physically copying and adding these files I get ArgumentException: The directory name 'Path-To-project\pagesettings' does not exist. (Parameter 'Path')
I can see why this is happening, but can't work out how to change it, or if it is possible.
The article linked above suggests adding code to the csproj file like:
<ItemGroup>
<Content Include="readme.txt">
<Pack>true</Pack>
<PackagePath>\</PackagePath>
</Content>
</ItemGroup>
But that doesn't work and in fact seems unnecessary since the Pack command is including my files, just not creating them properly when installing.
Also - it would be extremely handy if there was a way to tell VS to prompt whether to install this file or not. Since it is settings, if a user changes the settings and then later installs an updated version of my NuGet package I would not want it to overwrite their customised settings... perhaps this is why the link design happens... if so, if someone could confirm?
Actually, you should create a .props file in your nuget package.
1) create a file called <package_id>.props file in your nuget project.
Like this:
Note: if your created nuget package called test.1.0.0.nupkg, the file should be named as test.props so that it will work.
2) add these in the test.props file:
<Project>
<Target Name="CopyFiles" BeforeTargets="Build">
<ItemGroup>
<File Include="$(MSBuildThisFileDirectory)..\Pagesettings\*.*"></File>
</ItemGroup>
<Copy SourceFiles="#(File)" DestinationFolder="$(ProjectDir)Pagesettings"></Copy>
</Target>
</Project>
3) add these in xxx.csproj file:
<ItemGroup>
<None Include="Pagesettings\*.*(the json files' path of your nuget project)" Pack="true" PackagePath="Pagesettings">
</None>
<None Include="build\*.*" Pack="true" PackagePath="build"></None>
</ItemGroup>
then reapck your project.
4) then clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.
5) when you insall this new version of the nuget package, please build your main project again to run the target to generate the files.
Besides, there is also a similar issue about this.
I'm using CopyToPublishDirectory in my .csproj to copy over files/folders when publishing my dotnet app:
<None Update="Views\**\*; wwwroot\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Is there a way to copy over a folder and change its name in the process? For example, I'd like to copy over a subset of my node_modules folder, so I could create a new folder called node_modules_dev with my subset of npm dependencies, and copy it over via CopyToPublishDirectory as node_modules. I'd imagine the syntax would work something like this:
<None Update="node_modules_dev/**/*" Rename="node_modules">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Thanks--
The trick here is to:
Ensure that the items are not yet included by default so there is no leftover metadata from previous glob pattern expansions. This can be done by adding the path to the DefaultItemExcludes property so the web sdk will ignore the files.
Use the %(RecursiveDir) metadata that is available for items expanded via glob patterns and represents the value of any expanded path. This will be defined for the Include="…" syntax only hence 1.
This will overwrite the default target path to a new directory using the Link metadata:
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);node_modules_dev\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<Content Include="node_modules_dev\**\*" Link="node_nodules\%(RecursiveDir)%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>