简体   繁体   English

从csproj文件获取项目output路径

[英]Get project output path from csproj file

Is it possible, from code, to get some project's output path from its vb/csproj alone?是否有可能从代码中单独从其 vb/csproj 获取某些项目的 output 路径? I am using.Net framework (4.8).我正在使用 .Net 框架 (4.8)。 I could not find anything exactly matching what I am looking for.我找不到任何与我正在寻找的完全匹配的东西。 There is https://learn.microsoft.com/en-us/do.net/api/microsoft.build.evaluation.project.getpropertyvalue?view=msbuild-17.netcore but this isn't available in the.Net framework.https://learn.microsoft.com/en-us/do.net/api/microsoft.build.evaluation.project.getpropertyvalue?view=msbuild-17.netcore但这在 .Net 框架中不可用.

EDIT编辑

I am coding an output builder, which takes targeted assemblies of a solution projects and copy them in a specified location.我正在编写一个 output 构建器,它采用解决方案项目的目标程序集并将它们复制到指定位置。

The code below uses MSBuild to get the properties of a csproj file specified as a string, and retrieves the current TargetPath.下面的代码使用 MSBuild 获取指定为字符串的 csproj 文件的属性,并检索当前的 TargetPath。 If you create a Console App in .NET Framework 4.8 it will get the output library path of a .NET Framework project.如果您在 .NET Framework 4.8 中创建控制台应用程序,它将获得 .NET Framework 项目的 output 库路径。

using Microsoft.Build.Evaluation;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PathGetter
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string testCsproj = @"C:\Dotnet\WpfControlLibrary1\WpfControlLibrary1\WpfControlLibrary1.csproj";
            string result = GetProperty(testCsproj, "TargetPath");
            Console.WriteLine(result);
        }

        public static string GetProperty(string csproj, string propertyName)
        {
           using (var collection = new ProjectCollection())
            {
                var project = new Project(csproj, new Dictionary<string, string>(), 
                    null, collection, ProjectLoadSettings.Default);
                return project.Properties.Where(p => p.Name == propertyName)
                    .Select(p => p.EvaluatedValue).SingleOrDefault();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM