我有一些工具可以对.NET解决方案进行更新,但它们需要知道解决方案所在的目录。
我将这些工具添加为外部工具,它们将出现在IDE工具菜单中,并提供$(SolutionDir)
作为参数。这很有效。
然而,我希望用户能够通过自定义顶级菜单(我创建了一个Visual Studio集成包项目)和通过解决方案节点上的上下文菜单(我创建了一个Visual Studio插件项目)更轻松地访问这些工具。我正在寻找一种方法来从这些上下文中获取当前解决方案目录。
我尝试从VisualStudio.DTE
对象获取解决方案信息:
EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
但是,这返回的是插件的解决方案目录,而不是当前解决方案的目录。
我尝试回显$(SolutionDir)
并读取它:
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();
但是,这返回的是IDE的目录,而不是当前解决方案的目录。
在解决方案节点CommandBar
中,我没有看到任何相关信息。
或者,如果有一种可以以编程方式访问已定义的Visual Studio外部工具并启动它们(使用已定义的宏参数)的方法,则可以使用该方法。
解决方案是什么?