简体   繁体   English

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

[英]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. 我知道这听起来很奇怪,但是我想实现以下目标:我正在写一个VSIX扩展程序,该程序可以读取普通项目或解决方案本身中包含的所有文件。 To access solution files or solution folders Microsoft organizes them also in the DTE project collection. 要访问解决方案文件或解决方案文件夹,Microsoft也会在DTE项目集合中对其进行组织。 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. 可以看到,在我的解决方案中有3个文件:两个解决方案文件和一个项目项文件。

Now take a look when I access the DTE project collection: 现在查看当我访问DTE项目集合时:

在此处输入图片说明

As you can see the "projects" in the solution have no FullName. 如您所见,解决方案中的“项目”没有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. 在我的扩展程序中,我需要区分普通项目和“解决方案项目”,而我发现这样做的唯一方法是检查FullName属性是否为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? AND:解决方案文件或项目是否始终位于.sln文件所在的根目录中?

greetings Nico 问候尼科

Instead of using the DTE project collection, try moving up to the DTE Solution interface instead . 与其尝试使用DTE项目集合,不如尝试移至DTE 解决方案界面

As you can see from the API, the fullname property is found there, as well as the collection of projects. 如您从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;
      }
   }
}

Credit: Our friend Carlos Quintero was responsible for the code above. 信用:我们的朋友Carlos Quintero负责上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DllImport 从 c# 项目到相同解决方案中的 c++ 项目 - DllImport from c# project to c++ project in same solution 使用EnvDTE API从Visual Studio加载项内部以编程方式将文件添加为C#项目中的链接 - Programmaticaly add file as a link in C# project from inside visual studio add-in using EnvDTE API envdte configurationmanager Project StartOptions - envdte configurationmanager Project StartOptions EnvDTE - 如何从项目中的文件中获取文本 - EnvDTE - How to get the text from a file in a project ENVDTE - 将新项目添加到现有解决方案并将其定位到特定文件夹中 - ENVDTE - Add new project to existing solution and locate it in a specific folder 从 Visual Studio 解决方案中提取项目作为单独的项目 - Extracting a project as a separate project from the Visual Studio solution c#从同一解决方案中的其他项目运行表格 - c# run form from other project in same solution 从同一解决方案C#中的另一个项目中读取配置属性 - Read config properties from another project in the same solution C# 在一个解决方案中从VB项目调用C# - Calling a C# from a VB project in one solution 在同一解决方案中在C#项目和C ++项目之间进行交互 - interacting between a C# project and C++ project in same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM