[英]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;
dte.Solution.Open(path);
dte.ExecuteCommand("File.OpenProject", path);
dte.ExecuteCommand("File.OpenFolder", path);
dte.ExecuteCommand("File.OpenFile", path);
I hope that helps someone. 我希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.