internal const int ERROR_NO_UNICODE_TRANSLATION = 0x459;
internal const int ERROR_NOT_FOUND = 0x490;
internal const int ERROR_BAD_IMPERSONATION_LEVEL = 0x542;
- internal const int ERROR_NO_SYSTEM_RESOURCES = 0x5AA;
- internal const int E_FILENOTFOUND = unchecked((int)0x80070002);
+ internal const int ERROR_NO_SYSTEM_RESOURCES = 0x5AA;
internal const int ERROR_TIMEOUT = 0x000005B4;
}
}
/// <summary>Local Machine key. This key should be used as the root for all machine specific settings.</summary>
public static readonly RegistryKey LocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
-
+
+#if REGISTRY_ASSEMBLY
/// <summary>Classes Root Key. This is the root key of class information.</summary>
public static readonly RegistryKey ClassesRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default);
key.SetValue(valueName, value, valueKind);
}
}
+#endif
}
}
// Determine our session from what is in the registry.
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerName + "}";
- if (System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8)
+ if (IntPtr.Size == 8)
regKey = @"Software" + @"\Wow6432Node" + regKey;
else
regKey = @"Software" + regKey;
- var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
- if (key != null)
+ using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey))
{
- foreach (string valueName in key.GetValueNames())
+ if (key != null)
{
- if (valueName.StartsWith("ControllerData_Session_", StringComparison.Ordinal))
+ foreach (string valueName in key.GetValueNames())
{
- string strId = valueName.Substring(23); // strip of the ControllerData_Session_
- int etwSessionId;
- if (int.TryParse(strId, out etwSessionId))
+ if (valueName.StartsWith("ControllerData_Session_", StringComparison.Ordinal))
{
- // we need to assert this permission for partial trust scenarios
- (new RegistryPermission(RegistryPermissionAccess.Read, regKey)).Assert();
- var data = key.GetValue(valueName) as byte[];
- if (data != null)
+ string strId = valueName.Substring(23); // strip of the ControllerData_Session_
+ int etwSessionId;
+ if (int.TryParse(strId, out etwSessionId))
{
- var dataAsString = System.Text.Encoding.UTF8.GetString(data);
- int keywordIdx = dataAsString.IndexOf("EtwSessionKeyword", StringComparison.Ordinal);
- if (0 <= keywordIdx)
+ // we need to assert this permission for partial trust scenarios
+ (new RegistryPermission(RegistryPermissionAccess.Read, regKey)).Assert();
+ var data = key.GetValue(valueName) as byte[];
+ if (data != null)
{
- int startIdx = keywordIdx + 18;
- int endIdx = dataAsString.IndexOf('\0', startIdx);
- string keywordBitString = dataAsString.Substring(startIdx, endIdx-startIdx);
- int keywordBit;
- if (0 < endIdx && int.TryParse(keywordBitString, out keywordBit))
- action(etwSessionId, 1L << keywordBit, ref sessionList);
+ var dataAsString = System.Text.Encoding.UTF8.GetString(data);
+ int keywordIdx = dataAsString.IndexOf("EtwSessionKeyword", StringComparison.Ordinal);
+ if (0 <= keywordIdx)
+ {
+ int startIdx = keywordIdx + 18;
+ int endIdx = dataAsString.IndexOf('\0', startIdx);
+ string keywordBitString = dataAsString.Substring(startIdx, endIdx-startIdx);
+ int keywordBit;
+ if (0 < endIdx && int.TryParse(keywordBitString, out keywordBit))
+ action(etwSessionId, 1L << keywordBit, ref sessionList);
+ }
}
}
}
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN && PLATFORM_WINDOWS)
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerId + "}";
- if (System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8)
- regKey = @"HKEY_LOCAL_MACHINE\Software" + @"\Wow6432Node" + regKey;
+ if (IntPtr.Size == 8)
+ regKey = @"Software" + @"\Wow6432Node" + regKey;
else
- regKey = @"HKEY_LOCAL_MACHINE\Software" + regKey;
+ regKey = @"Software" + regKey;
string valueName = "ControllerData_Session_" + etwSessionId.ToString(CultureInfo.InvariantCulture);
#if !CORECLR
(new RegistryPermission(RegistryPermissionAccess.Read, regKey)).Assert();
#endif
- data = Microsoft.Win32.Registry.GetValue(regKey, valueName, null) as byte[];
- if (data != null)
+ using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey))
{
- // We only used the persisted data from the registry for updates.
- command = ControllerCommand.Update;
- return true;
+ data = key?.GetValue(valueName, null) as byte[];
+ if (data != null)
+ {
+ // We only used the persisted data from the registry for updates.
+ command = ControllerCommand.Update;
+ return true;
+ }
}
#endif
}
============================================================================*/
private static int GetAdvanceHijriDate()
{
- int hijriAdvance = 0;
- Microsoft.Win32.RegistryKey key = null;
-
- try
+ using (RegistryKey key = Registry.CurrentUser.OpenSubKey(InternationalRegKey))
{
- // Open in read-only mode.
- key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser).OpenSubKey(InternationalRegKey, false);
- }
- //If this fails for any reason, we'll just return 0.
- catch (ObjectDisposedException) { return 0; }
- catch (ArgumentException) { return 0; }
+ // Abort if we didn't find anything
+ if (key == null)
+ {
+ return 0;
+ }
- if (key != null)
- {
- try
+ object value = key.GetValue(HijriAdvanceRegKeyEntry, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
+ if (value == null)
{
- object value = key.InternalGetValue(HijriAdvanceRegKeyEntry, null, false, false);
- if (value == null)
- {
- return (0);
- }
- string str = value.ToString();
- if (string.Compare(str, 0, HijriAdvanceRegKeyEntry, 0, HijriAdvanceRegKeyEntry.Length, StringComparison.OrdinalIgnoreCase) == 0)
+ return 0;
+ }
+
+ int hijriAdvance = 0;
+ string str = value.ToString();
+ if (string.Compare(str, 0, HijriAdvanceRegKeyEntry, 0, HijriAdvanceRegKeyEntry.Length, StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ if (str.Length == HijriAdvanceRegKeyEntry.Length)
+ hijriAdvance = -1;
+ else
{
- if (str.Length == HijriAdvanceRegKeyEntry.Length)
- hijriAdvance = -1;
- else
+ try
{
- try
+ int advance = int.Parse(str.AsSpan(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture);
+ if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
{
- int advance = int.Parse(str.AsSpan(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture);
- if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
- {
- hijriAdvance = advance;
- }
+ hijriAdvance = advance;
}
- // If we got garbage from registry just ignore it.
- // hijriAdvance = 0 because of declaraction assignment up above.
- catch (ArgumentException) { }
- catch (FormatException) { }
- catch (OverflowException) { }
}
+ // If we got garbage from registry just ignore it.
+ // hijriAdvance = 0 because of declaraction assignment up above.
+ catch (ArgumentException) { }
+ catch (FormatException) { }
+ catch (OverflowException) { }
}
}
- finally
- {
- key.Close();
- }
+ return hijriAdvance;
}
- return (hijriAdvance);
}
}
}
{
public partial class JapaneseCalendar : Calendar
{
- private const string c_japaneseErasHive = @"System\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras";
- private const string c_japaneseErasHivePermissionList = @"HKEY_LOCAL_MACHINE\" + c_japaneseErasHive;
+ private const string JapaneseErasHive = @"System\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras";
// We know about 4 built-in eras, however users may add additional era(s) from the
// registry, by adding values to HKLM\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras
try
{
// Need to access registry
- RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine).OpenSubKey(c_japaneseErasHive, false);
-
- // Abort if we didn't find anything
- if (key == null) return null;
-
- // Look up the values in our reg key
- string[] valueNames = key.GetValueNames();
- if (valueNames != null && valueNames.Length > 0)
+ using (RegistryKey key = Registry.LocalMachine.OpenSubKey(JapaneseErasHive))
{
- registryEraRanges = new EraInfo[valueNames.Length];
+ // Abort if we didn't find anything
+ if (key == null) return null;
- // Loop through the registry and read in all the values
- for (int i = 0; i < valueNames.Length; i++)
+ // Look up the values in our reg key
+ string[] valueNames = key.GetValueNames();
+ if (valueNames != null && valueNames.Length > 0)
{
- // See if the era is a valid date
- EraInfo era = GetEraFromValue(valueNames[i], key.GetValue(valueNames[i]).ToString());
+ registryEraRanges = new EraInfo[valueNames.Length];
+
+ // Loop through the registry and read in all the values
+ for (int i = 0; i < valueNames.Length; i++)
+ {
+ // See if the era is a valid date
+ EraInfo era = GetEraFromValue(valueNames[i], key.GetValue(valueNames[i]).ToString());
- // continue if not valid
- if (era == null) continue;
+ // continue if not valid
+ if (era == null) continue;
- // Remember we found one.
- registryEraRanges[iFoundEras] = era;
- iFoundEras++;
+ // Remember we found one.
+ registryEraRanges[iFoundEras] = era;
+ iFoundEras++;
+ }
}
}
}
// PAL Layer ends here
- private static string[] s_japaneseErasEnglishNames = new string[] { "M", "T", "S", "H" };
+ private static readonly string[] s_japaneseErasEnglishNames = new string[] { "M", "T", "S", "H" };
private static string GetJapaneseEnglishEraName(int era)
{
// PAL Layer ends here
- private static string[] JapaneseErasEnglishNames = new string[] { "M", "T", "S", "H" };
+ private static readonly string[] s_JapaneseErasEnglishNames = new string[] { "M", "T", "S", "H" };
private static string GetJapaneseEnglishEraName(int era)
{
Debug.Assert(era > 0);
- return era <= JapaneseErasEnglishNames.Length ? JapaneseErasEnglishNames[era - 1] : " ";
+ return era <= s_JapaneseErasEnglishNames.Length ? s_JapaneseErasEnglishNames[era - 1] : " ";
}
private static bool GetJapaneseEraInfo(int era, out DateTimeOffset dateOffset, out string eraName, out string abbreviatedEraName)
// read LastEntry {(yearN, 1, 1) - MaxValue }
// read the FirstEntry and LastEntry key values (ex: "1980", "2038")
- int first = (int)dynamicKey.GetValue(FirstEntryValue, -1, RegistryValueOptions.None);
- int last = (int)dynamicKey.GetValue(LastEntryValue, -1, RegistryValueOptions.None);
+ int first = (int)dynamicKey.GetValue(FirstEntryValue, -1);
+ int last = (int)dynamicKey.GetValue(LastEntryValue, -1);
if (first == -1 || last == -1 || first > last)
{
private static unsafe bool TryGetTimeZoneEntryFromRegistry(RegistryKey key, string name, out REG_TZI_FORMAT dtzi)
{
- byte[] regValue = key.GetValue(name, null, RegistryValueOptions.None) as byte[];
+ byte[] regValue = key.GetValue(name, null) as byte[];
if (regValue == null || regValue.Length != sizeof(REG_TZI_FORMAT))
{
dtzi = default;
//
if (result)
{
- string registryStandardName = key.GetValue(StandardValue, string.Empty, RegistryValueOptions.None) as string;
+ string registryStandardName = key.GetValue(StandardValue, string.Empty) as string;
result = string.Equals(registryStandardName, timeZone.GetStandardName(), StringComparison.Ordinal);
}
return result;
daylightName = string.Empty;
// read the MUI_ registry keys
- string displayNameMuiResource = key.GetValue(MuiDisplayValue, string.Empty, RegistryValueOptions.None) as string;
- string standardNameMuiResource = key.GetValue(MuiStandardValue, string.Empty, RegistryValueOptions.None) as string;
- string daylightNameMuiResource = key.GetValue(MuiDaylightValue, string.Empty, RegistryValueOptions.None) as string;
+ string displayNameMuiResource = key.GetValue(MuiDisplayValue, string.Empty) as string;
+ string standardNameMuiResource = key.GetValue(MuiStandardValue, string.Empty) as string;
+ string daylightNameMuiResource = key.GetValue(MuiDaylightValue, string.Empty) as string;
// try to load the strings from the native resource DLL(s)
if (!string.IsNullOrEmpty(displayNameMuiResource))
// fallback to using the standard registry keys
if (string.IsNullOrEmpty(displayName))
{
- displayName = key.GetValue(DisplayValue, string.Empty, RegistryValueOptions.None) as string;
+ displayName = key.GetValue(DisplayValue, string.Empty) as string;
}
if (string.IsNullOrEmpty(standardName))
{
- standardName = key.GetValue(StandardValue, string.Empty, RegistryValueOptions.None) as string;
+ standardName = key.GetValue(StandardValue, string.Empty) as string;
}
if (string.IsNullOrEmpty(daylightName))
{
- daylightName = key.GetValue(DaylightValue, string.Empty, RegistryValueOptions.None) as string;
+ daylightName = key.GetValue(DaylightValue, string.Empty) as string;
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Security;
using System.Text;
namespace Microsoft.Win32
}
}
- // This dummy method is added to have the same implemenatation of Registry class.
- // Its not being used anywhere.
- public RegistryKey CreateSubKey(string subkey)
- {
- return null;
- }
-
private static void FixupPath(StringBuilder path)
{
Debug.Assert(path != null);
{
if (throwOnMissingValue)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSubKeyValueAbsent);
+ throw new ArgumentException(SR.Arg_RegSubKeyValueAbsent);
}
// Otherwise, just return giving no indication to the user.
// (For compatibility)
{
// We need to throw SecurityException here for compatibility reasons,
// although UnauthorizedAccessException will make more sense.
- ThrowHelper.ThrowSecurityException(ExceptionResource.Security_RegistryPermission);
+ throw new SecurityException(SR.Security_RegistryPermission);
}
return null;
{
if (dataStrings[i] == null)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSetStrArrNull);
+ throw new ArgumentException(SR.Arg_RegSetStrArrNull);
}
sizeInBytes = checked(sizeInBytes + (dataStrings[i].Length + 1) * 2);
}
}
catch (OverflowException)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSetMismatchedKind);
+ throw new ArgumentException(SR.Arg_RegSetMismatchedKind);
}
catch (InvalidOperationException)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSetMismatchedKind);
+ throw new ArgumentException(SR.Arg_RegSetMismatchedKind);
}
catch (FormatException)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSetMismatchedKind);
+ throw new ArgumentException(SR.Arg_RegSetMismatchedKind);
}
catch (InvalidCastException)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegSetMismatchedKind);
+ throw new ArgumentException(SR.Arg_RegSetMismatchedKind);
}
if (ret == 0)
{
if (_hkey == null)
{
- ThrowHelper.ThrowObjectDisposedException(_keyName, ExceptionResource.ObjectDisposed_RegKeyClosed);
+ throw new ObjectDisposedException(_keyName, SR.ObjectDisposed_RegKeyClosed);
}
}
EnsureNotDisposed();
if (!IsWritable())
{
- ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource.UnauthorizedAccess_RegistryNoWrite);
+ throw new UnauthorizedAccessException(SR.UnauthorizedAccess_RegistryNoWrite);
}
}
while (nextSlash != -1)
{
if ((nextSlash - current) > MaxKeyLength)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegKeyStrLenBug);
+ throw new ArgumentException(SR.Arg_RegKeyStrLenBug);
current = nextSlash + 1;
nextSlash = name.IndexOf("\\", current, StringComparison.OrdinalIgnoreCase);
}
if ((name.Length - current) > MaxKeyLength)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RegKeyStrLenBug);
+ throw new ArgumentException(SR.Arg_RegKeyStrLenBug);
}
private static void ValidateKeyView(RegistryView view)
{
if (view != RegistryView.Default && view != RegistryView.Registry32 && view != RegistryView.Registry64)
{
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidRegistryViewCheck, ExceptionArgument.view);
+ throw new ArgumentException(SR.Argument_InvalidRegistryViewCheck, nameof(view));
}
}
-
- // Win32 constants for error handling
- private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
- private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
- private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
}
// the name for this API is meant to mimic FileMode, which has similar values
Argument_ImplementIComparable,
Argument_InvalidType,
Argument_InvalidArgumentForComparison,
- Argument_InvalidRegistryKeyPermissionCheck,
ArgumentOutOfRange_NeedNonNegNum,
Arg_ArrayPlusOffTooSmall,
Arg_NonZeroLowerBound,
Arg_RankMultiDimNotSupported,
- Arg_RegKeyDelHive,
- Arg_RegKeyStrLenBug,
- Arg_RegSetStrArrNull,
- Arg_RegSetMismatchedKind,
- Arg_RegSubKeyAbsent,
- Arg_RegSubKeyValueAbsent,
Argument_AddingDuplicate,
Serialization_InvalidOnDeser,
InvalidOperation_EnumEnded,
NotSupported_SortedListNestedWrite,
InvalidOperation_NoValue,
- InvalidOperation_RegRemoveSubKey,
- Security_RegistryPermission,
- UnauthorizedAccess_RegistryNoWrite,
- ObjectDisposed_RegKeyClosed,
NotSupported_InComparableType,
- Argument_InvalidRegistryOptionsCheck,
- Argument_InvalidRegistryViewCheck,
InvalidOperation_NullArray,
Arg_MustBeType,
Arg_NeedAtLeast1Rank,