Can I use both wildcard and Link element inside the Compile element?

狂风中的少年 提交于 2019-12-10 03:54: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?


I am quite new to the Visual Studio Platform. Apologize in advance for any stupidity in the questions.


回答1:


<Content Include="..\..\SomeDirectory\**\*.xml">
  <Link>SomeLinkDirectoryOfYourChoosing\%(Filename)%(Extension)</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>



回答2:


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>



回答3:


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.

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

Sources:

  • https://github.com/Microsoft/msbuild/issues/2795
  • https://github.com/dotnet/sdk/pull/1246
  • Link Additional Files in Visual Studio



回答4:


To include subfolders:

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


来源:https://stackoverflow.com/questions/4623304/can-i-use-both-wildcard-and-link-element-inside-the-compile-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!