枚举.csproj(dotnet核心)中的文件 [英] Enumerate Files in a .csproj (dotnet core)

查看:61
本文介绍了枚举.csproj(dotnet核心)中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#构建工具,该工具应与Visual Studio/MSBuild一起使用. 我想枚举C#项目中的所有文件.项目格式是新的(.NET Core).csproj.

I am working on a build tool in C# that should work with Visual Studio / MSBuild. I want to enumerate all files that are part of a C# project. The project format is the new (.NET Core) .csproj.

描述项目系统的文档指出使用MSBuild(文件格式)或Common Project系统(项目树).我不熟悉这两个API.查看这些项目的文档并没有立即帮助.

The documentation describing the Project System points at using MSBuild (file format) or Common Project System (project tree). I'm unfamiliar with both APIs. Looking at documentation for those respective projects is not immediately helpful.

正如专家可能知道的那样,新的.csproj文件不会列出隐式属于项目的每个文件.另一方面,它可能会列出项目文件夹外部的链接"文件.我想确保获得所有被视为项目一部分的文件.

As the expert probably knows, the new .csproj file does not list every file that is implicitly part of the project. On the other hand it may list a 'linked' file that is outside the project folder. I want to make sure I get all files that are considered part of the project.

最终,我想专注于特定的文件类型(.json),但我认为一般问题值得一问.

Ultimately I want to focus on a particular file type (.json), but I thought the general question was worth asking.

总结:我如何编写一个C#库,利用适当的程序包(希望很容易)枚举csproj中的所有文件?

推荐答案

Buildalyzer 是最易于使用的软件包,它以.NETStandard 2.0为目标,使其成为跨平台的. (Omnisharp当前不提供用于处理工作空间的NuGet软件包.Microsoft.CodeAnalysis提出了正确获取正确引用的挑战,并且仅限于net46.)

Buildalyzer is the easiest package to use, and it targets .NETStandard 2.0 making it cross-platform. (Omnisharp does not currently offer a NuGet package for working with the workspace. And Microsoft.CodeAnalysis poses a challenge to get the correct references in place, and is limited to net46.)

using Buildalyzer;

private static IList<string> InlcudedProjectKeys = new[] { "None", "Compile", "Content", "EmbeddedResource" };

private static IEnumerable<string> EnumerateProjectFiles(string projectPath)
{
    AnalyzerManager manager = new AnalyzerManager();
    ProjectAnalyzer analyzer = manager.GetProject(projectPath);
    AnalyzerResults results = analyzer.Build();
    AnalyzerResult result = results.Single();

    // If only interested in C# files, check out:
    //string[] sourceFiles = result.SourceFiles;

    IReadOnlyDictionary<string, ProjectItem[]> items = result.Items;
    foreach (var item in items)
    {
        // Skip keys like ProjectReference that aren't for files
        if (!InlcudedProjectKeys.Contains(item.Key))
            continue;
        ProjectItem[] projectItems = item.Value;
        foreach (var projectItem in projectItems)
        {
            // The item spec for files will be the path relative to the project directory
            yield return projectItem.ItemSpec;
        }
    }
}

要获得奖励积分,只需获取*.json个文件:

And for bonus points, to get only *.json files:

var jsonFiles = EnumerateProjectFiles(projectPath)
    .Where(path => path.EndsWith(".json"))
    .ToArray();

感谢 Hitesh 链接到相关资源.

这篇关于枚举.csproj(dotnet核心)中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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