How to get project folder on a VSIX project

倾然丶 夕夏残阳落幕 提交于 2020-02-25 03:54:08

问题


I'm developing an extension for VS2015 and i need to get the project folder of the opened solution. And then find the bin folder to get the output DLL. All this because i need to instantiate a class from the output DLL using reflection.

I tried to get some information from msdn but it's difficult to me understand. Is there a way to do this?

Thanks in advance


回答1:


You could try something like this...

Find the project file of interest (maybe the startup project), or let the user select it. The project output directory is stored within it´s project file, so one should use MSBuild to read that information, or the better: let MSBuild evaluate the project assembly´s target path property. What you need would be the absolute path of the project file, or an EnvDTE.Project reference.

The absolute path to an output assembly can be obtained by evaluating the TargetPath property. You need to reference the Microsoft.Build.dll assembly, create a new project-collection and load the project by creating a new instance of the Microsoft.Build.Evaluation.Project class. This will allow you to query defined properties from the project and their evaluated values...

using Microsoft.Build.Evaluation;

...

public static string GetTargetPathFrom(EnvDTE.VsProject project)
{
    const string PropertyName = "TargetPath";
    return GetPropertyValueFrom(project.FileName, PropertyName);
}

public static string GetPropertyValueFrom(string projectFile, string propertyName)
{
    using (var projectCollection = new ProjectCollection())
    {
        var p = new Project(
            projectFile, 
            null, 
            null, 
            projectCollection, 
            ProjectLoadSettings.Default);

        return p.Properties
            .Where(x => x.Name == propertyName)
            .Select(x => x.EvaluatedValue)
            .SingleOrDefault();
        }
    }
}

The sample provided above will use the default project build configuration; I haven´t tried it, but it may work to change the Platform and Configuration properties by passing global properties to the Project ctor. You could try this...

...

var globalProperties = new Dictionary<string, string>()
    {
        { "Configuration", "Debug" }, 
        { "Platform", "AnyCPU" }
    };

var p = new Project(
    projectFile, 
    globalProperties, 
    null, 
    projectCollection,
    ProjectLoadSettings.Default);

...


来源:https://stackoverflow.com/questions/33013175/how-to-get-project-folder-on-a-vsix-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!