Created
November 2, 2021 10:03
-
-
Save svick/ffaa5d3fc3ef66479a6aa5811a3cd8e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Build" Version="16.11.0" ExcludeAssets="runtime" /> | |
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Xml; | |
using Microsoft.Build.Evaluation; | |
using Microsoft.Build.Locator; | |
// Note: code using MSBuild has to be in another method than the call to MSBuildLocator | |
MSBuildLocator.RegisterDefaults(); | |
string GetAppHostPath() | |
{ | |
var xml = @" | |
<Project Sdk=""Microsoft.NET.Sdk""> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
</Project>"; | |
var project = Project.FromXmlReader(XmlReader.Create(new StringReader(xml)), options: new()).CreateProjectInstance(); | |
project.Build("ProcessFrameworkReferences", loggers: null); | |
return project.GetItems("AppHostPack").Single().GetMetadata("Path").EvaluatedValue; | |
} | |
Console.WriteLine(GetAppHostPath()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment