EnvDTE项目将C#项目与解决方案项目分开

时间:2016-08-14 22:57:36

标签: c# vsix envdte

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

Test solution

所以你可以看到,在我的解决方案中有3个文件:两个解决方案文件和一个项目项目文件。

现在看看我访问DTE项目集合时:

enter image description here

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

问候 尼科

1 个答案:

答案 0 :(得分:0)

尝试向上移动到DTE Solution interface instead

,而不是使用DTE项目集

从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负责上述代码。

相关问题