编程正从加载项当前的Visual Studio IDE的解决方案目录 [英] Programmatically getting the current Visual Studio IDE solution directory from addins

查看:222
本文介绍了编程正从加载项当前的Visual Studio IDE的解决方案目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些工具,对.NET解决方案执行更新,但他们需要知道那里的解决方案所在的目录。



我添加了这些工具,外部工具,他们出现在IDE工具菜单,并提供 $(SolutionDir)作为参数。这工作得很好。



不过,我想这些工具更容易在IDE中获得通过自定义顶级菜单的用户(我创建一个Visual Studio集成总包项目),并通过解决方案节点的上下文菜单(为此我创建了一个Visual Studio外接程序项目)。我正在寻找一种方式来获得通过这些上下文当前的解决方案目录。



我试图让从 VisualStudio.DTE <解决方案的信息/ code>目标:

  EnvDTE.DTE DTE =(EnvDTE.DTE)System.Runtime.InteropServices.Marshal。 GetActiveObject(VisualStudio.DTE); 
串solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);



不过,这将返回解决方案目录的加载项,而不是当前的解决方案。



我试图呼应 $(SolutionDir)和回读:

  System.Diagnostics.ProcessStartInfo procStartInfo =新System.Diagnostics.ProcessStartInfo(CMD,回声$(SolutionDir)); 

//需要以下命令来重定向标准输出。
//这意味着,它会被重定向到Process.StandardOutput StreamReader的。
procStartInfo.RedirectStandardOutput = TRUE;
procStartInfo.UseShellExecute = FALSE;
//不要创建黑色窗口。
procStartInfo.CreateNoWindow = TRUE;
//现在我们创建一个过程,分配它的ProcessStartInfo并启动
的System.Diagnostics.Process PROC =新的System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
//获取输出到一个字符串
字符串结果= proc.StandardOutput.ReadToEnd();



不过,这回的目录的IDE,而不是当前的解决方案。



我没有看到解决方案节点命令栏的任何相关信息。



另外,如果有以编程方式访问定义的Visual Studio的外部工具,并将其发布(使用已定义的宏参数)的方式,将工作。



什么是该解决方案?


解决方案

EnvDTE.DTE DTE =
(EnvDTE.DTE)系统.Runtime.InteropServices.Marshal.GetActiveObject(VisualStudio.DTE);
串solutionDir =
System.IO.Path.GetDirectoryName(dte.Solution.FullName);



不过,这将返回该解决方案
目录的加载项,而不是
目前的解决方案。




您的方法来获取该目录是不错的。什么是错的是你得到的 VisualStudio.DTE 对象的方式。其中,被称为这个代码?我想这是在你的加载项。你在Visual Studio将打开Visual Studio中的另一个实例执行(调试)你的附加在你打开你的解决方案?所以,你有Visual Studio中的两个实例。



GetActiveObject(VisualStudio.DTE)得到一个随机的Visual Studio实例。你的情况,这显然是Visual Studio中的加载项项目,因为你得到的路径到你的加载项。这对于解释什么是你的问题的原因。



获得 DTE 正确的方法是非常简单的。事实上,您的加载在已经参考为DTE在其运行(即,在其中溶液被打开)。它存储在您的外接连接类的全局变量_applicationObject。当你的插件在的OnConnection 事件处理程序启动时,它被设置。因此,所有你需要的是拨打电话:

 字符串solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName); 


I have some tools that perform updates on .NET solutions, but they need to know the directory where the solution is located.

I added these tools as External Tools, where they appear in the IDE Tools menu, and supplying $(SolutionDir) as an argument. This works fine.

However, I want these tools to be easier to access in the IDE for the user through a custom top level menu (for which I created a Visual Studio integration package project) and through a context menu on solution nodes (for which I created a Visual Studio add-in project). I'm looking for a way to get the current solution directory through these contexts.

I tried getting the solution information from the VisualStudio.DTE object:

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

But, this returns the solution directory for the add ins, not the current solution.

I tried echoing $(SolutionDir) and reading it back:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "echo $(SolutionDir)");

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();

But, this returned the directory for the IDE, not the current solution.

I didn't see any relevant information in the solution node CommandBar.

Alternatively, if there was a way to programmatically access the defined Visual Studio external tools and launch them (using the already defined macro arguments), that would work.

What is the solution?

解决方案

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE"); string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

But, this returns the solution directory for the add ins, not the current solution.

Your approach to get the directory is good. What's wrong is the way you get the VisualStudio.DTE object. Where is this code called? I assume it is in your add-in. Do you execute (debug) your add-in in Visual Studio which opens another instance of Visual Studio where you open your solution? So you have two instances of Visual Studio.

The GetActiveObject("VisualStudio.DTE") gets a random Visual Studio instance. In your case, it is apparently Visual Studio with an add-in project since you get path to your add-in. That's for explanation what would be the reason of your problem.

The correct way to get DTE is very simple. In fact, your add-in already has reference to DTE in which it runs (that is, in which the solution is opened). It is stored in a global variable _applicationObject in your add-in connect class. It is set when your add-in starts in the OnConnection event handler. So all you need is to call:

string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);

这篇关于编程正从加载项当前的Visual Studio IDE的解决方案目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆