Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Files using Wildcard into a folder in Visual Studio

I am using

<ItemGroup>
  <EmbeddedResource Include="..\..\resources\hbm\*.hbm.xml" />
</ItemGroup>

to include a bunch of xml files into my C# project. Works fine.

But, I don't want them in the "root level" of my project, I would rather see them in a subfolder in my project.

For example, this file is included into a Mapping folder in Visual Studio:

<ItemGroup>
  <EmbeddedResource Include="Mapping\User.hbm.xml" />
</ItemGroup>

That's what I want for my *.hbm.xml files.

I can't figure out how to do it and still keep my wildcard *.hbm.xml part and also keep the actual files in a different directory.

I've looked at MSDN's doc on MSBUILD and items, but no luck.

like image 944
quip Avatar asked Sep 01 '09 20:09

quip


People also ask

How do I add a file path in Visual Studio?

In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.

What is inside .VS folder?

Usually, . vs folder is required by Visual Studio to store opened documents, breakpoints, and other information about state of your solution. which means It contains typical files like, Temporary caches used by Roslyn for IntelliSense.

How do I create a folder in Visual Studio 2019?

Put the folders into the bin (folder) > Debug or Release folder in your project folder. 3). Open your project in Visual Studio > click the Show All Files button > expand the bin , Debug > select and right-click the parent folder > choose Include in Project option.


2 Answers

Perhaps this has changed in MSBuild since the original answer was posted, but it is possible to use both wildcards and links at the same time. For example, I use the following block in a C# project to import data files into a test library.

<ItemGroup>
  <None Include="..\SOMENAME.Tests\data\**\*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <Link>data\%(RecursiveDir)%(Filename)%(Extension)</Link>
  </None>
</ItemGroup>

The only disadvantage I've seen so far is that MSBuild recreates the folder names on the file system (ie $(ProjectPath)\data\somesubfolder) which is a little annoying but not a huge issue.

As a test, I also tried the OP's request of embedding resources, using the following snippet, and again this seemed to work fine - dotPeek showed the resources were present in the compiled assembly in addition to being present in the Solution Explorer.

<ItemGroup>
  <EmbeddedResource  Include="..\SOMENAME.Tests\data\**\*.*">
      <Link>resources\%(RecursiveDir)%(Filename)%(Extension)</Link>
  </EmbeddedResource>
</ItemGroup>

(This was using Visual Studio 2013 and still works as of VS2019)

Update 08Jun2021: The above syntax works is fine for old style csproj files, but if you are using the new SDK format, the syntax is a little different, albeit simpler.

The OP's original question of wildcard embedding can be accomplished with the following

<ItemGroup>
  <EmbeddedResource Include="data\**\*.*" />
</ItemGroup>

And to have wildcard file copies for changed or missing files.

<ItemGroup>
  <None Update="data\**\*.*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>
like image 96
Richard Moss Avatar answered Oct 04 '22 20:10

Richard Moss


I think you can't use links and wildcard at the same time.

You could use this notation to link to include User.hbm.xml file in Mapping folder in Visual Studio :

<ItemGroup>
  <EmbeddedResource Include="..\..\resources\hbm\User.hbm.xml">
    <Link>Mapping\User.hbm.xml</Link>
  </EmbeddedResource>
</ItemGroup>

But you can't do that

<ItemGroup>
  <EmbeddedResource Include="..\..\resources\hbm\**\*.hbm.xml">
    <Link>%(RecursiveDir)\%(Filename)%(Extension)</Link>
  </EmbeddedResource>
</ItemGroup>
like image 23
Julien Hoarau Avatar answered Oct 04 '22 18:10

Julien Hoarau