找到你要的答案

Q:How to get project folder on a VSIX project

Q:如何获得项目文件夹在VSIX项目

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

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?

先谢谢了。

answer1: 回答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);

...

你可以试试这样的东西…

找到感兴趣的项目文件(可能是启动项目),或者让用户选择它。项目输出目录存储在它´的工程文件,所以应该使用MSBuild阅读信息,或更好:让MSBuild评估项目装配´的目标路径属性。你需要将项目文件的绝对路径,或者一个项目引用envdte。

输出组件的绝对路径可以通过获得目标路径性能评价。你需要参考的microsoft.build.dll组件,创建一个新项目的收集和负荷的microsoft.build.evaluation.project创建类的新实例的项目。这将允许您查询来自项目的定义属性和它们的评估值…

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

以上所提供的示例将使用默认的项目生成配置;我还´T尝试它,但它可能会改变工作平台,通过全球性质的项目的医生配置属性。你可以试试这个…

...

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

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

...
visual-studio-2015  visual-studio-extensions  vsix  vsx