-
Notifications
You must be signed in to change notification settings - Fork 1
/
VisualStudio.cs
83 lines (63 loc) · 1.89 KB
/
VisualStudio.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace vscmd {
public partial class VisualStudio : ComObject {
#region Constructors
private VisualStudio(object dte)
: base(dte) { }
const string ProgID = "VisualStudio.DTE";
enum HResult : uint {
MK_E_UNAVAILABLE = 0x800401E3, // Operation unavailable
}
public static VisualStudio GetOrCreate() {
dynamic dte = GetActiveObjectOrDefault();
if (dte != null)
return new VisualStudio(dte);
var type = Type.GetTypeFromProgID(ProgID);
dte = Activator.CreateInstance(type);
dte.UserControl = true;
return new VisualStudio(dte);
}
static object GetActiveObjectOrDefault() {
foreach (var progID in GetProgIDs()) {
try {
return Marshal.GetActiveObject(progID);
} catch (COMException err) {
if ((HResult)err.HResult != HResult.MK_E_UNAVAILABLE)
throw;
}
}
return null;
}
static IEnumerable<string> GetProgIDs() {
yield return ProgID;
const string prefix = ProgID + ".";
var versioned = Registry.ClassesRoot.GetSubKeyNames()
.Where(progID => progID.StartsWith(prefix))
.OrderByDescending(progID => float.Parse(progID.Substring(prefix.Length)));
foreach (var progID in versioned)
yield return progID;
}
#endregion
#region Window
public void ActivateMainWindow() {
this.Object.MainWindow.Activate();
}
#endregion
#region ExecuteCommand
public void OpenFile(FileInfo file) {
this.Object.ExecuteCommand("File.OpenFile", "\"" + file.FullName + "\"");
}
#endregion
#region Solution
dynamic Solution {
get { return this.Object.Solution; }
}
#endregion
}
}