Repository navigation
Expand file tree
/
Copy pathDriverUI.cs
More file actions
70 lines (65 loc) · 2.93 KB
/
Copy pathDriverUI.cs
File metadata and controls
70 lines (65 loc) · 2.93 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
using FastbootEnhance.Core.Usb;
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
namespace FastbootEnhance
{
/// <summary>
/// Samsung's USB driver: whether it is installed, and the window that downloads it from
/// Samsung, checks Samsung signed it and runs its installer. Download (Odin) mode needs it;
/// it is the same driver Odin itself uses, so installing it breaks nothing.
/// FASTBOOT_STUDIO_FAKE_SAMSUNG_DRIVER ("installed" or "missing") answers for tests.
/// </summary>
static class DriverUI
{
const string StandInVariable = "FASTBOOT_STUDIO_FAKE_SAMSUNG_DRIVER";
/// <summary>True when Windows' driver store holds the Download-mode driver, or the installer is listed in Programs.</summary>
public static bool SamsungInstalled()
{
string standIn = Environment.GetEnvironmentVariable(StandInVariable);
if (!string.IsNullOrEmpty(standIn))
return string.Equals(standIn.Trim(), "installed", StringComparison.OrdinalIgnoreCase);
try
{
string store = Path.Combine(Environment.SystemDirectory, "DriverStore", "FileRepository");
if (Directory.Exists(store) && Directory.EnumerateDirectories(store)
.Any(d => SamsungDriver.IsDriverStoreFolder(Path.GetFileName(d))))
return true;
}
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException)
{
}
const string uninstall = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
foreach (RegistryView view in new[] { RegistryView.Registry64, RegistryView.Registry32 })
{
try
{
using (RegistryKey root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view))
using (RegistryKey list = root.OpenSubKey(uninstall))
{
if (list == null)
continue;
foreach (string name in list.GetSubKeyNames())
{
using (RegistryKey entry = list.OpenSubKey(name))
{
if (SamsungDriver.IsProgramName(entry?.GetValue("DisplayName") as string))
return true;
}
}
}
}
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException)
{
}
}
return false;
}
/// <summary>Opens the download-and-install window; <paramref name="after"/> runs when it closes.</summary>
public static void InstallSamsung(Action after = null)
{
DriverWindow.Show(after);
}
}
}