简体   繁体   中英

Get project output path from csproj file

Is it possible, from code, to get some project's output path from its vb/csproj alone? I am using.Net framework (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.

EDIT

I am coding an output builder, which takes targeted assemblies of a solution projects and copy them in a specified location.

The code below uses MSBuild to get the properties of a csproj file specified as a string, and retrieves the current TargetPath. If you create a Console App in .NET Framework 4.8 it will get the output library path of a .NET Framework project.

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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