简体   繁体   中英

EnvDTE Project separate C# Project from solution project

I know it sounds weird but I want to achieve the following: I´m writing on a VSIX extension that reads all my files that are included in a normal project or in the solution itself. To access solution files or solution folders Microsoft organizes them also in the DTE project collection. Look at the following example:

测试溶液

So you can see, that in my solution there are 3 files: two solution files and one project item file.

Now take a look when I access the DTE project collection:

在此处输入图片说明

As you can see the "projects" in the solution have no FullName. In my extension I need to differ normal projects and "solution projects" and the only way I found to do this is to check if the FullName property is null. So I know this is a horrible solution, but do you know better ways to do that? AND: Are solution files or items always located in the root directory where the .sln file is located?

greetings Nico

Instead of using the DTE project collection, try moving up to the DTE Solution interface instead .

As you can see from the API, the fullname property is found there, as well as the collection of projects.

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

Credit: Our friend Carlos Quintero was responsible for the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM