csproj:如何获取所有资源? [英] csproj: how to get all resources?

查看:80
本文介绍了csproj:如何获取所有资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取所有资源,将副本复制到创建的二进制文件中?

How to get all recources,wich copies to created binary?

我认为这是所有这样的元素(具有CopyToOutputDirectory标签):

I think that it is all elements like this (has CopyToOutputDirectory tag):

<ItemGroup>
 <None Include="Configs\Config.config">
   <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </None>
</ItemGroup>

像这样:

<ItemGroup>
<Resource Include="Resources\Icons\icon4.png" />
</ItemGroup>
<ItemGroup>
  <Resource Include="Resources\Icons\icon5.png" />
</ItemGroup>
<ItemGroup>
  <Resource Include="Resources\Icons\icon6.png" />
</ItemGroup>
<ItemGroup>
  <Resource Include="icon7.ico" />
</ItemGroup>
<ItemGroup>
  <Resource Include="Resources\Icons\icon8.png" />
</ItemGroup>

我应该这样解析带有标签 Resource的所有元素(?):

And i should parse all elements with tag "Resource" like this (?):

XDocument doc = XDocument.Load(filePath);
        IEnumerable<XAttribute> attr = doc.Descendants().Attributes("Resource");

另一个问题-如何在CopyToOutputDirectory标记之前获取元素?

And another question- how to get element before CopyToOutputDirectory tag?

PS如果有用的话-我有一个包含项目的文件夹(另一个文件夹)。
我解析了此文件夹中的所有.csproj文件,并生成了XML文件,其中包含的资源列表已复制到每个项目的已编译二进制文件中。

P.S. if it be useful- i have folder with projects(another folders). I parse all .csproj files from this folders and generate XML file with list of resources wich copied to compiled binary of each project.

推荐答案

项目文件不是不是个简单的XML文件,它们可以包含需要评估的逻辑。

Project files are NOT simple XML files, they can contain logic which needs to be evaluated.

您可以通过以下方式实现使用 Microsoft.Build 程序集和 Microsoft.Build.Evaluation 命名空间。

You can achieve that by using the Microsoft.Build assembly and the Microsoft.Build.Evaluation namespace.

var project = new Project(@"..\..\StackOverflow.csproj");

        var itemsToCopy = new List<ProjectItem>();

        var projectItems = project.Items;
        foreach (var projectItem in projectItems)
        {
            // e.g get all elements with CopyToOutputDirectory == "Always"
            var projectMetadata = projectItem.GetMetadata("CopyToOutputDirectory");
            if (projectMetadata != null && projectMetadata.EvaluatedValue == "Always")
                itemsToCopy.Add(projectItem);
        }

        foreach (var projectItem in itemsToCopy)
        {
            // e.g get then Include-Attribute from <None Include="Configs\Config.config">
            var evaluatedInclude = projectItem.EvaluatedInclude;
        }

        // get the resources that are not set to CopyToOutputDirectory == "Always"
        var collection = project.GetItems("Resources");
        var resources = collection.Except(itemsToCopy);
        foreach (var projectItem in resources)
        {
            // e.g get then Include-Attribute from <Resource Include="Resources\Icons\icon8.png" />
            var evaluatedInclude = projectItem.EvaluatedInclude;
        }

更新

这应该给出一个大致的概念,即如何对项目文件执行某些任务。

This should give a general idea how to do certain tasks with project files.

这篇关于csproj:如何获取所有资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆