0

I need to open a Visual Studio solution from java app, and I want to make sure that this VS project is not already open before opening it.

I thought about 2 solutions:

One, Check if the VS .sln file is locked by another process (see the answers to this question). The problem with this solution is that I don't know who is locking the file, it can be any other application like notepad, file explorer (on copying) etc.

Two, Find the running VS processes and check if this particular solution was sent to one of them as an argument (meaning was open by VS). Here is the code:

Process p = Runtime.getRuntime().exec("wmic process where caption=\"devenv.exe\" get commandline");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null){
        if(line.trim().contains(solutionPath))
               //the VS solution is already open
      }

The problem is that the output of this wmic command is in the form: <devenv.exe path> <VS project path> and this VS project path can be relative, referring to project file instead of solution file etc. For example, if I need to open the project C:\Users\username\My Documents\Visual Studio 2012\Projects\ProjectName\ProjectName.sln and the command line I get from wmic is "<devenv.exe path>" "C:\Users\username\Documents\Visual Studio 2012\Projects\ProjectName\ProjectName.vcxproj", both paths refer to the same VS project, but I won't know that.

EDIT: I just found that Visual Studio doesn't lock the solution file when it's open, so my first solution is not applicable. and as you can see in the comments below, the second one is not an option as project which was open or close within VS won't show up correctly. So I need a different way.

Any idea?

Community
  • 1
  • 1
user3114639
  • 1,895
  • 16
  • 42
  • 1
    With the second option, what would happen if you ran Visual Studio with no arguments, and then opened the project from within? I don't think that would show up... – Andrew Williamson Dec 28 '15 at 16:16
  • You're right. And the same problem will be if I ran Visual Studio with arguments, and then close the project from within- it will showed in the commandline although the project is close. So I need different solution. – user3114639 Dec 28 '15 at 17:15
  • Looks like you have little choice then. In what situation would someone else, besides Visual Studio, have a lock on the project file? – Andrew Williamson Dec 28 '15 at 17:19
  • For example, in my computer, Syncplicity service (https://en.wikipedia.org/wiki/Syncplicity) locks the files on synchronization. – user3114639 Dec 28 '15 at 19:17
  • Would you need to run your java application on the same file, at the same time, as one of these services? – Andrew Williamson Dec 28 '15 at 19:34
  • Yes, the service is running in the background all the time and looking for unsynchronized files to sync with. – user3114639 Dec 28 '15 at 19:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99151/discussion-between-andrew-williamson-and-user3114639). – Andrew Williamson Dec 28 '15 at 19:37

2 Answers2

0

Not foolproof, but you can enumerate top window titles and check for your "SolutionName - Microsoft Visual Studio".

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • Very unreliable, as the window title includes only the solution name and not the full path so it's not guaranteed at all that this is the same solution. – user3114639 Dec 29 '15 at 11:01
0

Visual Studio provides a COM interface that you can use to get a running Visual Studio instance and check the dte.Solution.FullName property. See for example Getting EnvDTE.DTE instance outside Visual Studio IDE.

Community
  • 1
  • 1
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • First, note that the code in this example is in C#/.Net, and I need java code. Also, the answer there suggests to create a new Visual Studio instance, and this is exactly what I want to avoid from... – user3114639 Dec 29 '15 at 10:35
  • Sorry, can't help with java code. Calling GetActiveObject doesn't create a new VS instance, it returns a reference to a running one. – Sergey Vlasov Dec 30 '15 at 05:43
  • Take a look in the answer there and see that If you use GetActiveObject(...) and there are two Visual Studio instances launched, you don't know whther the correct instance is returned, so it's recommended to create a new Visual Studio instance using System.Activator.CreateInstance(). – user3114639 Dec 30 '15 at 10:52