23

In the .csrpoj file, If I have

<Compile Include="c:\path\File1.cs">
  <Link>Dir1\File1.cs</Link>
</Compile>

Then Visual Studio shows that file as a shortcut under Dir1 folder in the Solution Explorer.

If I have

<Compile Include="c:\path\*.cs"></Compile>

Then all .cs files show up as shortcuts in Solution Explorer at top level:

Is there a way to include all files in some folder and make then show up under a sub-folder? Omitting the filename in Link element does not work:

<Compile Include="c:\path\*.cs">
  <Link>Dir1\</Link>
</Compile>

The files still show up at top level.

How do I include all files in a folder and still use the Link element? The reason I need this is, I need to include files from multiple folders and some of them have the same name. Two files at top level cannot have the same name.

Any other way to achieve this?

5
  • @leppie I am able to use Wildcards without the Link element. Jan 7, 2011 at 7:48
  • But as I said, not possible for a Link child. Have you tried <Link>Dir1\*.cs</Link> ?
    – leppie
    Jan 7, 2011 at 7:50
  • Yes, I tried <Link>Dir1\*.cs</Link>; VS complains of invalid characters. Jan 7, 2011 at 8:14
  • Then it is not possible, like I said.
    – leppie
    Jan 7, 2011 at 8:35
  • So how do I include all files from multiple folders that is not in project hierarchy considering there may be duplicate names? Jan 7, 2011 at 8:40

4 Answers 4

29

Others have suggested the using the Link attribute with placeholders, which indeed works. However, Microsoft has implemented a new attribute (which isn't in any of my code completion suggestions), named LinkBase, shown below.

<ItemGroup>
   <Compile Include="..\SomeDirectory\*.cs" LinkBase="SomeDirectoryOfYourChoosing" />
</ItemGroup>

Sources:

1
  • 2
    This is the best, leanest answer :)
    – Fabzter
    Nov 6, 2018 at 21:59
23
<Content Include="..\..\SomeDirectory\**\*.xml">
  <Link>SomeLinkDirectoryOfYourChoosing\%(Filename)%(Extension)</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
4
  • This does not work as is for me. But if I give a dummy name instead of wildcards then it works: <Content Include="..\..\primary-key-delete-then-insert\generated*.cs"> <Link>fse\xyz.cs</Link> <CopyToOutputDirectory>Never</CopyToOutputDirectory> </Content> Note dummy name xyz.cs. The files show up under folder fse in solution explorer Apr 4, 2011 at 17:13
  • It should be mentioned that any property change of included file links in VS replaces this single general items definition with set of them (for each included file). Mar 5, 2014 at 11:32
  • Just to add one more note to this: if you are using the <Content> tag, the form %(Filename)%(Extension) won't work. That would be if you are, for example, trying to have a set of files compiled. However, for anyone trying to get files copied to their output directory, using the <None> tag, the %(Filename)%(Extension) form will work (and if you're lucky will be slightly less confusing to the next person to look at it).
    – rrreee
    Apr 4, 2016 at 15:56
  • I had a similar requirement that I solved with: <None Include="..\..\..\configs\Backend***"> <Link>Config\%(RecursiveDir)\xx')</Link> </None>
    – spartaco
    Feb 28 at 14:37
6

For the sake of others, here's the answer plus the comment from Dean and Miserable Variable, which I found useful:

I have two projects, and I need to include *.xsd from one in the other, without copying the files or having to update the referencing csproj file every time a new XSD is added to the first.

The solution was to add the following to the csproj file

  <Content Include="..\BusinessLayer\Schemas\*.xsd">
    <Link>Contract\Schemas\xxx.xsd</Link>
  </Content>

Note xxx.xsd, you have to give a dummy filename in the Link element. It just gets replaced.

Also, you can include all sub folders with:

  <Content Include="..\BusinessLayer\Schemas\**\*.xsd">
    <Link>Contract\Schemas\ThisTextDoesntMatter</Link>
  </Content>

And files of all type (useful for pulling in CSS/JS/Style folders from 3rd parties) with:

  <Content Include="..\PresentationLayer\CustomerStyle\**\*.*">
    <Link>CustomerStyle\placeHolder</Link>
  </Content>
2

To include subfolders:

<ItemGroup>
  <Compile Include="..\SomeExternalFolder\**\*.cs" LinkBase="YourProjectFolder" />
</ItemGroup>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.