4

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.

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. It works fine, but it launches another Visual Studio to open the given file.

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

How to do it?

astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • 1
    Possible duplicate of [Programatically open file in visual studio](http://stackoverflow.com/questions/21785363/programatically-open-file-in-visual-studio) – Tamás Szabó Apr 13 '17 at 07:39
  • 1
    @TamásSzabó, thank you, but I want to open *solution* (as you would normally open in manually in VS), not to open solution single file as text file. The linked question uses VS as merely text editor, so useful, but it is not my goal. – astrowalker Apr 13 '17 at 08:07
  • 1
    True. I started looking into replacing the /edit with [/command](https://msdn.microsoft.com/en-us/library/19sf6kk3.aspx) and passing [OpenSolution](https://msdn.microsoft.com/en-us/library/1891wawd.aspx) as a command, but it also opens a new VS. I have to go now, but maybe you can find something there. Good luck! – Tamás Szabó Apr 13 '17 at 08:19

1 Answers1

2

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

"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.

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

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.

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
  • GetGlobalService returns null. I'm guessing there's a reference or version conflict somewhere. I'm referencing Microsoft.VisualStudio.Shell.15.0.dll – CoderForHire Aug 17 '20 at 23:55
  • It should be `AsyncPackage.GetGlobalService`. Also make sure that you're not trying to get the value of DTE in the package's constructor. – Yann Duran Aug 18 '20 at 12:51
  • This is what I have.Still getting null back static void Main(string[] args) { var dte = AsyncPackage.GetGlobalService(typeof(_DTE)) as DTE2; var solutionFile = @"C:\Projects\my.sln"; dte.Solution.Open(solutionFile); } – CoderForHire Aug 19 '20 at 00:51
  • @Yann Duran Is it possible to go to the beginning of the function definition, after openning the file by the method stated above ? – Siddhi Kamat Aug 06 '21 at 06:09
  • @sidso do you mean to open another file? – Yann Duran Aug 06 '21 at 13:07
  • @Yann Duran No, when the editor opens for the specified file. It should directly navigate the user to the line where the function definition starts (function name should be specified by the user before opening the editor for that file). – Siddhi Kamat Aug 06 '21 at 17:01
  • @sidso sorry, there's no way to manipulate it to do that – Yann Duran Aug 07 '21 at 03:49
  • Can you use EnvDTE on a standard Console APP (.NET 6)? or is it limited to only VSIX projects? – Carlos Torrecillas Nov 04 '22 at 13:25
  • @CarlosTorrecillas I could be wrong, but AFAIK it's not using any VS-specific services that would require it to be called from within VS. You should be able to use by it simply adding it as a Nuget package, and then calling it, but if it were me I think I'd be using the .NET file handling methods over using DTE. It really depends on what you're trying to accomplish. – Yann Duran Nov 05 '22 at 03:26
  • Thanks for the tips @YannDuran ! I may be wrong but it looks like you can use the DTE classes under a VSIX project otherwise when you get the GetGlobalService you get NULL back for that object? I have tried many combinations and packages but it looks like it does not "work". Is that or I have not found the right combination of Nuget packages (note I tried the VS.SDK, Shell.15, etc). Any ideas? – Carlos Torrecillas Nov 07 '22 at 08:12
  • @CarlosTorrecillas yes I think you're right. DTE is of course only for use in VSIX projects. Its purpose is to manipulate, or get data from, parts of Visual Studio. If you were to use old-school automation methods of getting a reference to the the running instance of Visual Studio, there may be a DTE property (or something along those lines). Methods like GetGlobalService should also be able to be called. It's been too long for me to remember anything more precise about automating VS from another application. HTH – Yann Duran Mar 05 '23 at 11:08