簡體   English   中英

EnvDTE項目將C#項目與解決方案項目分開

[英]EnvDTE Project separate C# Project from solution project

我知道這聽起來很奇怪,但是我想實現以下目標:我正在寫一個VSIX擴展程序,該程序可以讀取普通項目或解決方案本身中包含的所有文件。 要訪問解決方案文件或解決方案文件夾,Microsoft也會在DTE項目集合中對其進行組織。 看下面的例子:

測試溶液

可以看到,在我的解決方案中有3個文件:兩個解決方案文件和一個項目項文件。

現在查看當我訪問DTE項目集合時:

在此處輸入圖片說明

如您所見,解決方案中的“項目”沒有FullName。 在我的擴展程序中,我需要區分普通項目和“解決方案項目”,而我發現這樣做的唯一方法是檢查FullName屬性是否為null。 所以我知道這是一個可怕的解決方案,但是您知道更好的方法嗎? AND:解決方案文件或項目是否始終位於.sln文件所在的根目錄中?

問候尼科

與其嘗試使用DTE項目集合,不如嘗試移至DTE 解決方案界面

如您從API所見,可以在其中找到fullname屬性以及項目集合。

這是一個例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM