Search code examples
c#visual-studiovisual-studio-2017visual-studio-extensions

Visual Studio Extension: translate $(XXX) programmatically


I try to make an extension for VisualStudio 2017.

Currently, I don't find a mean to translate the $(XXX) (by example $(SolutionDir) or $(TargetName)) programmatically using C# and Visual.VCProject API.

EnvDTE.Project project = (EnvDTE.Project)selectedProjects.GetValue(0);
var vcproj = project.Object as VCProject;
VCConfiguration cfg = (VCConfiguration)vcproj.ActiveConfiguration;
VCDebugSettings debug = (VCDebugSettings)cfg.DebugSettings;

string path = debug.WorkingDirectory;
// Here path is $(ProjectDir) But I need something like c:\myProject
// Resolution can be done because Visual can do: my question is : How ?

Do you have some ideas to do that ?

References & doc:

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vcconfiguration.debugsettings.aspx

EDIT: After some search

I found some explain in doc about $()

https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-reference-the-name-or-location-of-the-project-file

May be reply is inside this part of doc ?

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.interop.ivsbuildpropertystorage(v=vs.140).aspx

https://learn.microsoft.com/fr-fr/visualstudio/extensibility/internals/persisting-data-in-the-msbuild-project-file

https://learn.microsoft.com/fr-fr/visualstudio/extensibility/internals/using-msbuild


Solution

  • Use VCConfiguration.Evaluate to valuate the value of a project model or environment macro

    EnvDTE.Project project = (EnvDTE.Project)selectedProjects.GetValue(0);
    var vcproj = project.Object as VCProject;
    VCConfiguration cfg = (VCConfiguration)vcproj.ActiveConfiguration;
    VCDebugSettings debug = (VCDebugSettings)cfg.DebugSettings;
    
    //string path = debug.WorkingDirectory;
    string path = cfg.Evaluate(debug.WorkingDirectory);
    
    // Here path is $(ProjectDir) But I need something like c:\myProject
    // Resolution can be done because Visual can do: my question is : How ?