2016-08-14 205 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負責上面的代碼。

相關問題