コード例 #1
0
ファイル: HijriCalendar.Win32.cs プロジェクト: ywscr/coreclr
        /*=================================GetAdvanceHijriDate==========================
         **Action: Gets the AddHijriDate value from the registry.
         **Returns:
         **Arguments:    None.
         **Exceptions:
         **Note:
         **  The HijriCalendar has a user-overidable calculation.  That is, use can set a value from the control
         **  panel, so that the calculation of the Hijri Calendar can move ahead or backwards from -2 to +2 days.
         **
         **  The valid string values in the registry are:
         **      "AddHijriDate-2"  =>  Add -2 days to the current calculated Hijri date.
         **      "AddHijriDate"    =>  Add -1 day to the current calculated Hijri date.
         **      ""              =>  Add 0 day to the current calculated Hijri date.
         **      "AddHijriDate+1"  =>  Add +1 days to the current calculated Hijri date.
         **      "AddHijriDate+2"  =>  Add +2 days to the current calculated Hijri date.
         **============================================================================*/
        private static int GetAdvanceHijriDate()
        {
            int hijriAdvance = 0;

            Microsoft.Win32.RegistryKey key = null;

            try
            {
                // Open in read-only mode.
                // Use InternalOpenSubKey so that we avoid the security check.
                key = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER).OpenSubKey(InternationalRegKey, false);
            }
            //If this fails for any reason, we'll just return 0.
            catch (ObjectDisposedException) { return(0); }
            catch (ArgumentException) { return(0); }

            if (key != null)
            {
                try
                {
                    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)
                    {
                        if (str.Length == HijriAdvanceRegKeyEntry.Length)
                        {
                            hijriAdvance = -1;
                        }
                        else
                        {
                            str = str.Substring(HijriAdvanceRegKeyEntry.Length);
                            try
                            {
                                int advance = Int32.Parse(str.ToString(), CultureInfo.InvariantCulture);
                                if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
                                {
                                    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) { }
                        }
                    }
                }
                finally
                {
                    key.Close();
                }
            }
            return(hijriAdvance);
        }
コード例 #2
0
        // 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
        //
        // Registry values look like:
        //      yyyy.mm.dd=era_abbrev_english_englishabbrev
        //
        // Where yyyy.mm.dd is the registry value name, and also the date of the era start.
        // yyyy, mm, and dd are the year, month & day the era begins (4, 2 & 2 digits long)
        // era is the Japanese Era name
        // abbrev is the Abbreviated Japanese Era Name
        // english is the English name for the Era (unused)
        // englishabbrev is the Abbreviated English name for the era.
        // . is a delimiter, but the value of . doesn't matter.
        // '_' marks the space between the japanese era name, japanese abbreviated era name
        //     english name, and abbreviated english names.
        private static EraInfo[] GetJapaneseEras()
        {
            // Look in the registry key and see if we can find any ranges
            int iFoundEras = 0;

            EraInfo[] registryEraRanges = null;

            try
            {
                // Need to access registry
                RegistryKey key = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).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)
                {
                    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;
                        }

                        // Remember we found one.
                        registryEraRanges[iFoundEras] = era;
                        iFoundEras++;
                    }
                }
            }
            catch (System.Security.SecurityException)
            {
                // If we weren't allowed to read, then just ignore the error
                return(null);
            }
            catch (System.IO.IOException)
            {
                // If key is being deleted just ignore the error
                return(null);
            }
            catch (System.UnauthorizedAccessException)
            {
                // Registry access rights permissions, just ignore the error
                return(null);
            }

            //
            // If we didn't have valid eras, then fail
            // should have at least 4 eras
            //
            if (iFoundEras < 4)
            {
                return(null);
            }

            //
            // Now we have eras, clean them up.
            //
            // Clean up array length
            Array.Resize(ref registryEraRanges, iFoundEras);

            // Sort them
            Array.Sort(registryEraRanges, CompareEraRanges);

            // Clean up era information
            for (int i = 0; i < registryEraRanges.Length; i++)
            {
                // eras count backwards from length to 1 (and are 1 based indexes into string arrays)
                registryEraRanges[i].era = registryEraRanges.Length - i;

                // update max era year
                if (i == 0)
                {
                    // First range is 'til the end of the calendar
                    registryEraRanges[0].maxEraYear = GregorianCalendar.MaxYear - registryEraRanges[0].yearOffset;
                }
                else
                {
                    // Rest are until the next era (remember most recent era is first in array)
                    registryEraRanges[i].maxEraYear = registryEraRanges[i - 1].yearOffset + 1 - registryEraRanges[i].yearOffset;
                }
            }

            // Return our ranges
            return(registryEraRanges);
        }
コード例 #3
0
        private static EraInfo[] GetErasFromRegistry()
        {
            int index = 0;

            EraInfo[] array = null;
            try
            {
                PermissionSet set = new PermissionSet(PermissionState.None);
                set.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras"));
                set.Assert();
                RegistryKey key = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).OpenSubKey(@"System\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras", false);
                if (key == null)
                {
                    return(null);
                }
                string[] valueNames = key.GetValueNames();
                if ((valueNames != null) && (valueNames.Length > 0))
                {
                    array = new EraInfo[valueNames.Length];
                    for (int j = 0; j < valueNames.Length; j++)
                    {
                        EraInfo eraFromValue = GetEraFromValue(valueNames[j], key.GetValue(valueNames[j]).ToString());
                        if (eraFromValue != null)
                        {
                            array[index] = eraFromValue;
                            index++;
                        }
                    }
                }
            }
            catch (SecurityException)
            {
                return(null);
            }
            catch (IOException)
            {
                return(null);
            }
            catch (UnauthorizedAccessException)
            {
                return(null);
            }
            if (index < 4)
            {
                return(null);
            }
            Array.Resize <EraInfo>(ref array, index);
            Array.Sort <EraInfo>(array, new Comparison <EraInfo>(JapaneseCalendar.CompareEraRanges));
            for (int i = 0; i < array.Length; i++)
            {
                array[i].era = array.Length - i;
                if (i == 0)
                {
                    array[0].maxEraYear = 0x270f - array[0].yearOffset;
                }
                else
                {
                    array[i].maxEraYear = (array[i - 1].yearOffset + 1) - array[i].yearOffset;
                }
            }
            return(array);
        }
コード例 #4
0
        private static EraInfo[] GetErasFromRegistry()
        {
            int newSize = 0;

            EraInfo[] array = (EraInfo[])null;
            try
            {
                PermissionSet      permissionSet      = new PermissionSet(PermissionState.None);
                RegistryPermission registryPermission = new RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras");
                permissionSet.AddPermission((IPermission)registryPermission);
                permissionSet.Assert();
                RegistryKey registryKey = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).OpenSubKey("System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras", false);
                if (registryKey == null)
                {
                    return((EraInfo[])null);
                }
                string[] valueNames = registryKey.GetValueNames();
                if (valueNames != null)
                {
                    if (valueNames.Length != 0)
                    {
                        array = new EraInfo[valueNames.Length];
                        for (int index = 0; index < valueNames.Length; ++index)
                        {
                            EraInfo eraFromValue = JapaneseCalendar.GetEraFromValue(valueNames[index], registryKey.GetValue(valueNames[index]).ToString());
                            if (eraFromValue != null)
                            {
                                array[newSize] = eraFromValue;
                                ++newSize;
                            }
                        }
                    }
                }
            }
            catch (SecurityException ex)
            {
                return((EraInfo[])null);
            }
            catch (IOException ex)
            {
                return((EraInfo[])null);
            }
            catch (UnauthorizedAccessException ex)
            {
                return((EraInfo[])null);
            }
            if (newSize < 4)
            {
                return((EraInfo[])null);
            }
            Array.Resize <EraInfo>(ref array, newSize);
            Array.Sort <EraInfo>(array, new Comparison <EraInfo>(JapaneseCalendar.CompareEraRanges));
            for (int index = 0; index < array.Length; ++index)
            {
                array[index].era = array.Length - index;
                if (index == 0)
                {
                    array[0].maxEraYear = 9999 - array[0].yearOffset;
                }
                else
                {
                    array[index].maxEraYear = array[index - 1].yearOffset + 1 - array[index].yearOffset;
                }
            }
            return(array);
        }