2016-08-14 207 views
0

我知道这听起来很奇怪,但我想实现以下目标: 我正在写一个VSIX扩展,它读取所有我的文件包含在一个普通项目或解决方案本身。 要访问解决方案文件或解决方案文件夹,Microsoft还会在DTE项目集合中组织它们。 请看下面的例子:EnvDTE项目从解决方案项目单独C#项目

Test solution

所以你可以看到,在我的解决方案有3个文件:两个解决方案文件和一个工程项目文件。当我访问DTE项目集合

现在来看看:

enter image description here

正如你可以看到“项目”的解决方案没有全名。 在我的扩展中,我需要区别普通项目和“解决方案项目”,唯一的办法是检查FullName属性是否为空。 所以我知道这是一个可怕的解决方案,但是你知道更好的方法吗? AND:解决方案文件或项目是否始终位于.sln文件所在的根目录中?

问候 尼科

回答

0

而不是使用DTE项目集合,试图向上移动到DTE Solution interface instead

正如您从API中看到的那样,可以在那里找到fullname属性以及项目集合。

Here's an example:

using System.Runtime.InteropServices; 
using System.Windows.Forms; 
using Microsoft.VisualStudio; 
using Microsoft.VisualStudio.Shell.Interop; 
using Microsoft.VisualStudio.OLE.Interop; 
using Microsoft.VisualStudio.Shell; 

namespace Company.MyVSPackage 
{ 
    // Only load the package if there is a solution loaded 
    [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)] 
    [PackageRegistration(UseManagedResourcesOnly = true)] 
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] 
    [Guid(GuidList.guidMyVSPackagePkgString)] 
    public sealed class MyVSPackagePackage : Package 
    { 
     public MyVSPackagePackage() 
     { 
     } 

     protected override void Initialize() 
     { 
     base.Initialize(); 

     ShowSolutionProperties(); 
     } 

     private void ShowSolutionProperties() 
     { 
     SVsSolution solutionService; 
     IVsSolution solutionInterface; 
     bool isSolutionOpen; 
     string solutionDirectory; 
     string solutionFullFileName; 
     int projectCount; 

     // Get the Solution service 
     solutionService = (SVsSolution)this.GetService(typeof(SVsSolution)); 

     // Get the Solution interface of the Solution service 
     solutionInterface = solutionService as IVsSolution; 

     // Get some properties 

     isSolutionOpen = GetPropertyValue<bool>(solutionInterface, __VSPROPID.VSPROPID_IsSolutionOpen); 
     MessageBox.Show("Is Solution Open: " + isSolutionOpen); 

     if (isSolutionOpen) 
     { 
      solutionDirectory = GetPropertyValue<string>(solutionInterface, __VSPROPID.VSPROPID_SolutionDirectory); 
      MessageBox.Show("Solution directory: " + solutionDirectory); 

      solutionFullFileName = GetPropertyValue<string>(solutionInterface, __VSPROPID.VSPROPID_SolutionFileName); 
      MessageBox.Show("Solution full file name: " + solutionFullFileName); 

      projectCount = GetPropertyValue<int>(solutionInterface, __VSPROPID.VSPROPID_ProjectCount); 
      MessageBox.Show("Project count: " + projectCount.ToString()); 
     } 
     } 

     private T GetPropertyValue<T>(IVsSolution solutionInterface, __VSPROPID solutionProperty) 
     { 
     object value = null; 
     T result = default(T); 

     if (solutionInterface.GetProperty((int)solutionProperty, out value) == Microsoft.VisualStudio.VSConstants.S_OK) 
     { 
      result = (T)value; 
     } 
     return result; 
     } 
    } 
} 

信用:我们的朋友Carlos Quintero负责上面的代码。

相关问题