-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenExecutableCommand.cs
More file actions
99 lines (85 loc) · 3.37 KB
/
OpenExecutableCommand.cs
File metadata and controls
99 lines (85 loc) · 3.37 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using CodingWithCalvin.OpenInNotepadPlusPlus.Dialogs;
using CodingWithCalvin.OpenInNotepadPlusPlus.Helpers;
using CodingWithCalvin.Otel4Vsix;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Windows.Forms;
using static CodingWithCalvin.OpenInNotepadPlusPlus.VSCommandTableVsct;
namespace CodingWithCalvin.OpenInNotepadPlusPlus.Commands
{
internal sealed class OpenExecutableCommand
{
private readonly Package _package;
private readonly SettingsDialogPage _settings;
private OpenExecutableCommand(Package package, SettingsDialogPage settings)
{
this._package = package;
this._settings = settings;
var commandService = (OleMenuCommandService)
ServiceProvider.GetService(typeof(IMenuCommandService));
if (commandService != null)
{
var menuCommandId = new CommandID(
guidOpenInNppCmdSet.Guid,
guidOpenInNppCmdSet.OpenInNpp
);
var menuItem = new MenuCommand(OpenPath, menuCommandId);
commandService.AddCommand(menuItem);
}
}
public static OpenExecutableCommand Instance { get; private set; }
private IServiceProvider ServiceProvider => this._package;
public static void Initialize(Package package, SettingsDialogPage settings)
{
Instance = new OpenExecutableCommand(package, settings);
}
private void OpenPath(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
using var activity = VsixTelemetry.StartCommandActivity("OpenInNotepadPlusPlus.OpenPath");
var service = (DTE2)this.ServiceProvider.GetService(typeof(DTE));
try
{
var selectedFilePath = ProjectHelpers.GetSelectedPath(service);
var executablePath = _settings.FolderPath;
if (
!string.IsNullOrEmpty(selectedFilePath) && !string.IsNullOrEmpty(executablePath)
)
{
VsixTelemetry.LogInformation("Opening file in Notepad++");
OpenExecutable(executablePath, selectedFilePath);
}
else
{
VsixTelemetry.LogError("Could not resolve path!");
MessageBox.Show("Couldn't resolve the folder");
}
}
catch (Exception ex)
{
activity?.RecordError(ex);
VsixTelemetry.TrackException(ex, new Dictionary<string, object>
{
{ "operation.name", "OpenPath" },
{ "command.name", "OpenInNotepadPlusPlus" }
});
}
}
private static void OpenExecutable(string executablePath, string selectedFilePath)
{
var startInfo = new ProcessStartInfo
{
FileName = $"\"{executablePath}\"",
Arguments = $"\"{selectedFilePath}\"",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
using (System.Diagnostics.Process.Start(startInfo)) {}
}
}
}