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?