forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowseForPrivateKey.cs
63 lines (57 loc) · 1.71 KB
/
BrowseForPrivateKey.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
using System.IO;
using System.Windows.Forms;
using GitCommands;
namespace GitUI
{
/// <summary>
/// Shows a dialog to let the user browse for a SSH key.
/// </summary>
public static class BrowseForPrivateKey
{
/// <summary>
/// Prompts the user to browse for a key, and attempts to load it. Returns the path to the key, if successful.
/// </summary>
public static string? BrowseAndLoad(IWin32Window parent)
{
var path = Browse(parent);
if (!string.IsNullOrEmpty(path))
{
if (LoadKey(parent, path))
{
return path;
}
}
return null;
}
/// <summary>
/// Prompts the user to browse for a key. Returns the path chosen, or null.
/// </summary>
public static string? Browse(IWin32Window parent)
{
using OpenFileDialog dialog = new()
{
Filter = " (*.ppk)|*.ppk",
InitialDirectory = ".",
Title = "Browse for key"
};
if (dialog.ShowDialog(parent) == DialogResult.OK)
{
return dialog.FileName;
}
return null;
}
/// <summary>
/// Tries to load the given key. Returns whether successful.
/// </summary>
public static bool LoadKey(IWin32Window parent, string? path)
{
if (!File.Exists(AppSettings.Pageant))
{
MessageBoxes.PAgentNotFound(parent);
return false;
}
GitModule.StartPageantWithKey(path);
return true;
}
}
}