简体   繁体   English

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

[英]How to programmatically open project/solution in already running Visual Studio?

The "possible duplicate" aims at opening the file, like in Notepad, I am looking for opening .csproj/.sln file not as just raw text file, but as full project/solution, so for example if my path is for .sln file, entire solution should be opened, not just literally single .sln file. “可能重复”旨在打开文件,就像在记事本中一样,我正在寻找打开.csproj / .sln文件而不仅仅是原始文本文件,而是作为完整的项目/解决方案,例如,如果我的路径是.sln文件,应该打开整个解决方案,而不仅仅是单个.sln文件。

I open project/solution this way: 我这样打开项目/解决方案:

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

where path is path to project/solution. 其中path是项目/解决方案的路径。 It works fine, but it launches another Visual Studio to open the given file. 它工作正常,但它启动另一个 Visual Studio来打开给定的文件。

I would like to open project/solution in already running VS (if there is none, in such case, sure launch VS as well). 我想在已经运行的VS中打开项目/解决方案(如果没有,在这种情况下,确保启动VS)。

How to do it? 怎么做?

I'm adding this in case anyone is still looking for the answer to this question, as I was in 2019. 我正在添加这个,万一有人还在寻找这个问题的答案,就像我在2019年那样。

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

I've also added how to open a folder or a file as well, just for completeness. 我还添加了如何打开文件夹文件 ,只是为了完整性。

Make sure to surround the code containing these statement in a try/catch , to handle path not found etc. 确保在try/catch包含包含这些语句的代码,以处理未找到的路径等。

Assuming: 假设:

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

  var dte = Package.GetGlobalService(typeof(_DTE)) as DTE2;
  1. To programmatically open a solution : 以编程方式打开解决方案
   dte.Solution.Open(path);
  1. To programmatically open a project : 以编程方式打开项目
  dte.ExecuteCommand("File.OpenProject", path);
  1. To programmatically open a folder : 以编程方式打开文件夹
  dte.ExecuteCommand("File.OpenFolder", path);
  1. To programmatically open a file : 以编程方式打开文件
  dte.ExecuteCommand("File.OpenFile", path);

I hope that helps someone. 我希望能有所帮助。

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

相关问题 以编程方式升级Visual Studio解决方案和项目文件 - Upgrade Visual Studio solution and project files programmatically 如何启动visual studio + open solution + 启动项目 - How to launch visual studio + open solution + launch project 如何以编程方式保存Visual Studio解决方案? - How to programmatically save Visual Studio solution? Visual Studio 中打开的项目/解决方案和打开的网站有哪些区别? - which are the differences between open project/solution and open website in Visual Studio? 如何使用 c# 在 Visual Studio 2010 中以编程方式将现有项目添加到当前解决方案 - How to add an existing project to a current solution programmatically in visual studio 2010 using c# 如何以编程方式在Visual Studio项目中生成新的Visual Studio项目? - How to Generate a New Visual Studio Project in a Visual Studio Project Programmatically? 以编程方式生成Visual Studio解决方案 - Programmatically generate Visual Studio Solution 在Visual Studio中以编程方式重新加载解决方案 - Programmatically reload solution in Visual Studio 尝试在Visual Studio中打开解决方案项目时收到导入错误 - Receiving import error when trying to open project of solution in Visual Studio 如何在不打开 Visual Studio 中关联解决方案的情况下打开项目? - How can I open a project without opening its associated solution in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM