forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsShellUtil.cs
81 lines (74 loc) · 2.65 KB
/
OsShellUtil.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
using System;
using System.Windows.Forms;
using GitCommands;
namespace GitUI
{
public static class OsShellUtil
{
/// <summary>
/// Open a file with its associated default application.
/// </summary>
/// <param name="filePath">Pathname of the file to open.</param>
public static void Open(string filePath)
{
try
{
new Executable(filePath).Start(useShellExecute: true, throwOnErrorExit: false);
}
catch (Exception)
{
OpenAs(filePath);
}
}
/// <summary>
/// Let the user chose an application to open a file.
/// </summary>
/// <param name="filePath">Pathname of the file to open.</param>
public static void OpenAs(string filePath)
{
// filePath must not be quoted
new Executable("rundll32.exe").Start("shell32.dll,OpenAs_RunDLL " + filePath, redirectOutput: true, outputEncoding: System.Text.Encoding.UTF8);
}
public static void SelectPathInFileExplorer(string filePath)
{
OpenWithFileExplorer($"/select, {filePath.Quote()}", quote: false);
}
public static void OpenWithFileExplorer(string arguments, bool quote = true)
{
new Executable("explorer.exe").Start(quote ? arguments.Quote() : arguments);
}
/// <summary>
/// opens urls even with anchor.
/// </summary>
public static void OpenUrlInDefaultBrowser(string? url)
{
if (!string.IsNullOrWhiteSpace(url))
{
new Executable(url).Start(useShellExecute: true, throwOnErrorExit: false);
}
}
/// <summary>
/// Prompts the user to select a directory.
/// </summary>
/// <param name="ownerWindow">The owner window.</param>
/// <param name="selectedPath">The initially selected path.</param>
/// <returns>The path selected by the user, or null if the user cancels the dialog.</returns>
public static string? PickFolder(IWin32Window ownerWindow, string? selectedPath = null)
{
using (FolderBrowserDialog dialog = new())
{
if (selectedPath is not null)
{
dialog.SelectedPath = selectedPath;
}
var result = dialog.ShowDialog(ownerWindow);
if (result == DialogResult.OK)
{
return dialog.SelectedPath;
}
}
// return null if the user cancelled
return null;
}
}
}