0

How to programmatically determine if my project is .NET Foundation or .NET Standard? Or, alternatively, how to programmatically determine if Xamarin.IOS or Windows Forms.

This is for a .Net Standard class lib that will run on either .NET Xamarin.IOS on an iPhone or a .NET Windows forms app on a Windows 10 laptop.

The app needs to know which of these platforms it is on.

Doug Null
  • 7,989
  • 15
  • 69
  • 148

1 Answers1

0

Microsoft Visual Studio uses GUIDs to identify a Project Type. As mentioned in tekeye blog post:

This allows for developers in different companies to call projects by the same name, but if they come across each other's work the same named projects would still be uniquely identifiable by Visual Studio.

GUIDs generated for project types are placed in .csproj file under ProjectTypeGuids and .sln file under Project.

In .csproj file:

<ProjectTypeGuids>{FEACFBD2-3405-455C-9665-78FE426C6842};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

In .sln file:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsFormsTestApp", "WindowsFormsTestAppWindowsFormsTestApp.csproj", "{F00515D6-939F-4E92-8FFD-A5D8F811FF84}"

You can find a complete list of GUIDs in Tekeye blog post linked above.

To grab this programmatically from .csproj file you can use snippet below:

XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load("projectPath\file.csproj");
IEnumerable<string> references = projDefinition
    .Element(msbuild + "Project")
    .Elements(msbuild + "PropertyGroup")
    .Elements(msbuild + "ProjectTypeGuids")  
    .Select(refElem => refElem.Value);

If you didn't find ProjectTypeGuids in .csproj file, you can find Guid in .sln file by parsing the file using regex as @watbywbarif did in this stack thread.

  • That seems complex and I'm concerned will take hours of development time. I'm looking to call some .Net function that returns a code that indicates the app is Windows Forms or Xamarin.IOS or .Net Foundation or .Net Standard. – Doug Null Sep 24 '19 at 14:51
  • Unfortunately it's the only way to achieve your goal. But there's another way that I don't think it's the solution that suites your problem or even you'll like it. You can use visual studio automation to make a add-in to get access to project properties window in c#, and then call that add-in in your code. But i don't think that's a good idea. –  Sep 25 '19 at 15:10