fix: used default instead of null for better type safety

This commit is contained in:
Notselwyn 2023-07-17 16:57:14 +02:00
parent 7fc31f6e90
commit e99bdabc51

View file

@ -91,34 +91,55 @@ namespace SafeExamBrowser.SystemComponents.Registry
public bool TryGetNames(string key, out IEnumerable<string> names) public bool TryGetNames(string key, out IEnumerable<string> names)
{ {
names = null; names = default;
RegistryKey keyObj; RegistryKey keyObj;
if (!TryOpenKey(key, out keyObj)) if (!TryOpenKey(key, out keyObj))
return false; return false;
bool success = true;
using (keyObj) using (keyObj)
{ {
names = keyObj.GetValueNames(); try
{
names = keyObj.GetValueNames();
}
catch (Exception e)
{
logger.Error($"Failed to get registry value names '{key}'!", e);
success = false;
// persist keyObj dispose operation by finishing using() {}
}
} }
return true; return success;
} }
public bool TryGetSubKeys(string key, out IEnumerable<string> subKeys) public bool TryGetSubKeys(string key, out IEnumerable<string> subKeys)
{ {
subKeys = null; subKeys = default;
RegistryKey keyObj; RegistryKey keyObj;
if (!TryOpenKey(key, out keyObj)) if (!TryOpenKey(key, out keyObj))
return false; return false;
bool success = true;
using (keyObj) using (keyObj)
{ {
subKeys = keyObj.GetSubKeyNames(); try
{
subKeys = keyObj.GetSubKeyNames();
}
catch (Exception e)
{
logger.Error($"Failed to get registry value names '{key}'!", e);
success = false;
// persist keyObj dispose operation by finishing using() {}
}
} }
return true; return success;
} }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) private void Timer_Elapsed(object sender, ElapsedEventArgs e)
@ -150,8 +171,8 @@ namespace SafeExamBrowser.SystemComponents.Registry
// yoinked (and partially modified to follow SEB conventions) private Win32 function: https://stackoverflow.com/a/58547945 // yoinked (and partially modified to follow SEB conventions) private Win32 function: https://stackoverflow.com/a/58547945
private bool GetBaseKeyFromKeyName(string keyName, out RegistryKey hiveKey, out string subKeyName) private bool GetBaseKeyFromKeyName(string keyName, out RegistryKey hiveKey, out string subKeyName)
{ {
hiveKey = null; hiveKey = default;
subKeyName = null; subKeyName = default;
string basekeyName; string basekeyName;
int i = keyName.IndexOf('\\'); int i = keyName.IndexOf('\\');
@ -217,7 +238,7 @@ namespace SafeExamBrowser.SystemComponents.Registry
/// </summary> /// </summary>
private bool TryOpenKey(string key, out RegistryKey keyObj) private bool TryOpenKey(string key, out RegistryKey keyObj)
{ {
keyObj = null; keyObj = default;
string subHiveKey; string subHiveKey;
try try
@ -228,7 +249,10 @@ namespace SafeExamBrowser.SystemComponents.Registry
keyObj = hiveObj.OpenSubKey(subHiveKey); keyObj = hiveObj.OpenSubKey(subHiveKey);
if (keyObj == null) if (keyObj == null)
{
keyObj = default;
return false; return false;
}
} }
catch (Exception e) catch (Exception e)
{ {