如何在已经运行的 Visual Studio 中以编程方式打开项目/解决方案? [英] How to programmatically open project/solution in already running Visual Studio?

查看:57
本文介绍了如何在已经运行的 Visual Studio 中以编程方式打开项目/解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的副本"旨在打开文件,就像在记事本中一样,我正在寻找打开 .csproj/.sln 文件,而不仅仅是原始文本文件,而是完整的项目/解决方案,例如,如果我的路径是 .sln 文件,应该打开整个解决方案,而不仅仅是单个 .sln 文件.

我以这种方式打开项目/解决方案:

I open project/solution this way:

var psi = new System.Diagnostics.ProcessStartInfo(path);
System.Diagnostics.Process.Start(psi);

其中 path 是项目/解决方案的路径.它工作正常,但它会启动另一个 Visual Studio 来打开给定的文件.

where path is path to project/solution. It works fine, but it launches another Visual Studio to open the given file.

我想在已经运行的 VS 中打开项目/解决方案(如果没有,在这种情况下,请确保也启动 VS).

I would like to open project/solution in already running VS (if there is none, in such case, sure launch VS as well).

怎么做?

推荐答案

我添加这个,以防有人仍在寻找这个问题的答案,就像我在 2019 年一样.

I'm adding this in case anyone is still looking for the answer to this question, as I was in 2019.

"如何以编程方式打开项目/解决方案 在已经运行的 Visual Studio 中?"

"How to programmatically open project/solution in already running Visual Studio?"

为了完整起见,我还添加了如何打开文件夹文件.

I've also added how to open a folder or a file as well, just for completeness.

确保将包含这些语句的代码括在 try/catch 中,以处理 path not found 等.

Make sure to surround the code containing these statement in a try/catch, to handle path not found etc.

假设:

  using EnvDTE;
  using EnvDTE80;
  using Microsoft.VisualStudio.Shell;

  var dte = Package.GetGlobalService(typeof(_DTE)) as DTE2;

  1. 以编程方式打开解决方案:

   dte.Solution.Open(path);

  1. 以编程方式打开项目:

  dte.ExecuteCommand("File.OpenProject", path);

  1. 以编程方式打开文件夹:

  dte.ExecuteCommand("File.OpenFolder", path);

  1. 以编程方式打开文件:

  dte.ExecuteCommand("File.OpenFile", path);

我希望对某人有所帮助.

I hope that helps someone.

这篇关于如何在已经运行的 Visual Studio 中以编程方式打开项目/解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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