-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathRegistry.fs
67 lines (55 loc) · 2.81 KB
/
Registry.fs
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
/// Copyright (c) Microsoft Corporation.
/// Utilities for setting the registry values
[<AutoOpen>]
module Microsoft.FSharpLu.Registry
open Microsoft.FSharpLu.Logging
/// Function to open registry base key and subkey for certain bitness types.
let private openKeyForBitness (hive : Microsoft.Win32.RegistryHive) (keyPath : string) (bitness : ExecutableInfo.ExeType) =
match bitness with
| ExecutableInfo.ExeType.x86 ->
Microsoft.Win32.RegistryKey.OpenBaseKey(hive, Microsoft.Win32.RegistryView.Registry32).OpenSubKey(keyPath, true)
| ExecutableInfo.ExeType.amd64 ->
Microsoft.Win32.RegistryKey.OpenBaseKey(hive, Microsoft.Win32.RegistryView.Registry64).OpenSubKey(keyPath, true)
| _ -> failwith "Unsupported bittness for registry operations!"
/// Set DWORD value under HKEY_CURRENT_USER path.
let setHkcuDwordValue (keyPath:string) (name:string) (value:int) =
use key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath, true)
if not (isNull key) then
key.SetValue(name, value, Microsoft.Win32.RegistryValueKind.DWord)
else
Trace.info "Key %s missing, registry value not updated." keyPath
/// Set DWORD value under HKEY_LOCAL_MACHINE path.
let setHklmDwordValue (keyPath:string) (name:string) (value:int) =
use key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(keyPath, true)
if not (isNull key) then
key.SetValue(name, value, Microsoft.Win32.RegistryValueKind.DWord)
else
Trace.info "Key %s missing, registry value not updated." keyPath
/// Set DWORD value in registry for specified hive and key according to bitness.
let setRegistryDwordValueEx (hive : Microsoft.Win32.RegistryHive) (keyPath : string) (name : string) (value : int) (bitness:ExecutableInfo.ExeType) =
use key =
openKeyForBitness hive keyPath bitness
if not (isNull key) then
key.SetValue(name, value, Microsoft.Win32.RegistryValueKind.DWord)
else
Trace.info "Key %s missing, registry value not updated." keyPath
/// Delete registry value from specified hive and key regardless the key type
let deleteRegistryValue (hive : Microsoft.Win32.RegistryHive) (keyPath : string) (name : string) (bitness : ExecutableInfo.ExeType) =
use key =
openKeyForBitness hive keyPath bitness
if not (isNull key) then
key.DeleteValue(name)
/// Function to obtain registry DWORD value from specified hive and key.
let tryGetRegistryDwordValue (hive : Microsoft.Win32.RegistryHive) (keyPath : string) (name : string) (bitness : ExecutableInfo.ExeType) =
use key =
openKeyForBitness hive keyPath bitness
let returnValue =
if isNull key then
None
else
let value = key.GetValue(name)
if isNull value then
None
else
Some (value :?> int)
returnValue