private Tuple <CultureInfo, String> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (_isDisposed)
            {
                return(new Tuple <CultureInfo, string>(null, null));
            }

            try
            {
                new FileIOPermission(FileIOPermissionAccess.Read, lexiconFilePath).Demand();
            }
            catch (SecurityException se)
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), se);
            }

            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath));
            }

            bool   fileCopied             = false;
            string lexiconPrivateCopyPath = null;

            try
            {
                CultureInfo culture = null;

                // Read the first line of the file and detect culture, if specified
                using (FileStream stream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    string line = null;
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        line    = reader.ReadLine();
                        culture = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }

                // Make a temp file and copy the original file over.
                // Ensure that the copy has Unicode (UTF16-LE) encoding
                lexiconPrivateCopyPath = WinRTSpellerInterop.GetTempFileName(extension: "dic");

                new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, lexiconPrivateCopyPath).Assert();
                try
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, lexiconPrivateCopyPath);
                    fileCopied = true;
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }

                // Add the temp file (with .dic extension) just created to a cache,
                // then pass it along to IUserDictionariesRegistrar

                _customDictionaryFilesLock.WaitOne();
                try
                {
                    if (!_customDictionaryFiles.ContainsKey(culture))
                    {
                        _customDictionaryFiles[culture] = new List <string>();
                    }

                    _customDictionaryFiles[culture].Add(lexiconPrivateCopyPath);
                }
                finally
                {
                    _customDictionaryFilesLock.Release();
                }

                SpellCheckerFactory.RegisterUserDictionary(lexiconPrivateCopyPath, culture.IetfLanguageTag);

                return(new Tuple <CultureInfo, string>(culture, lexiconPrivateCopyPath));
            }
            catch (Exception e) when((e is SecurityException) || (e is ArgumentException) || !fileCopied)
            {
                // IUserDictionariesRegistrar.RegisterUserDictionary can
                // throw ArgumentException on failure. Cleanup the temp file if
                // we successfully created one.
                if (lexiconPrivateCopyPath != null)
                {
                    File.Delete(lexiconPrivateCopyPath);
                }

                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), e);
            }
        }
        /// <summary>
        ///     Actual implementation of loading a dictionary
        /// </summary>
        /// <param name="lexiconFilePath"></param>
        /// <param name="dictionaryLoadedCallback"></param>
        /// <param name="callbackParam"></param>
        /// <returns>
        ///     A tuple of cultureinfo detected from <paramref name="lexiconFilePath"/> and
        ///     a temp file path which holds a copy of <paramref name="lexiconFilePath"/>
        ///
        ///     If no culture is specified in the first line of <paramref name="lexiconFilePath"/>
        ///     in the format #LID nnnn (where nnnn = decimal LCID of the culture), then invariant
        ///     culture is returned.
        /// </returns>
        /// <remarks>
        ///     At the end of this method, we guarantee that <paramref name="lexiconFilePath"/>
        ///     can be reclaimed (i.e., potentially deleted) by the caller.
        /// </remarks>
        private Tuple<string, string> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (_isDisposed)
            {
                return new Tuple<string, string>(null, null);
            }

            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath));
            }

            bool fileCopied = false;
            string lexiconPrivateCopyPath = null;

            try
            {
                CultureInfo culture = null;

                // Read the first line of the file and detect culture, if specified
                using (FileStream stream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    string line = null;
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        line = reader.ReadLine();
                        culture = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }

                string ietfLanguageTag = culture.IetfLanguageTag;

                // Make a temp file and copy the original file over.
                // Ensure that the copy has Unicode (UTF16-LE) encoding
                using (FileStream lexiconPrivateCopyStream = FileHelper.CreateAndOpenTemporaryFile(out lexiconPrivateCopyPath, extension: "dic"))
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, lexiconPrivateCopyStream);
                    fileCopied = true;
                }

                // Add the temp file (with .dic extension) just created to a cache,
                // then pass it along to IUserDictionariesRegistrar

                if (!_customDictionaryFiles.ContainsKey(ietfLanguageTag))
                {
                    _customDictionaryFiles[ietfLanguageTag] = new List<string>();
                }

                _customDictionaryFiles[ietfLanguageTag].Add(lexiconPrivateCopyPath);

                using (new SpellerCOMActionTraceLogger(this, SpellerCOMActionTraceLogger.Actions.RegisterUserDictionary))
                {
                    SpellCheckerFactory.RegisterUserDictionary(lexiconPrivateCopyPath, ietfLanguageTag);
                }

                return new Tuple<string, string>(ietfLanguageTag, lexiconPrivateCopyPath);
            }
            catch (Exception e) when ((e is ArgumentException) || !fileCopied)
            {
                // IUserDictionariesRegistrar.RegisterUserDictionary can
                // throw ArgumentException on failure. Cleanup the temp file if
                // we successfully created one.
                if (lexiconPrivateCopyPath != null)
                {
                    FileHelper.DeleteTemporaryFile(lexiconPrivateCopyPath);
                }

                throw new ArgumentException(SR.Get(SRID.CustomDictionaryFailedToLoadDictionaryUri, lexiconFilePath), e);
            }
        }
        private Tuple <string, string> LoadDictionaryImpl(string lexiconFilePath)
        {
            if (this._isDisposed)
            {
                return(new Tuple <string, string>(null, null));
            }
            try
            {
                new FileIOPermission(FileIOPermissionAccess.Read, lexiconFilePath).Demand();
            }
            catch (SecurityException innerException)
            {
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }), innerException);
            }
            if (!File.Exists(lexiconFilePath))
            {
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }));
            }
            bool   flag = false;
            string text = null;
            Tuple <string, string> result;

            try
            {
                CultureInfo cultureInfo = null;
                using (FileStream fileStream = new FileStream(lexiconFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        string line = streamReader.ReadLine();
                        cultureInfo = WinRTSpellerInterop.TryParseLexiconCulture(line);
                    }
                }
                string ietfLanguageTag = cultureInfo.IetfLanguageTag;
                using (FileStream fileStream2 = FileHelper.CreateAndOpenTemporaryFile(out text, FileAccess.Write, FileOptions.None, "dic", "WPF"))
                {
                    WinRTSpellerInterop.CopyToUnicodeFile(lexiconFilePath, fileStream2);
                    flag = true;
                }
                if (!this._customDictionaryFiles.ContainsKey(ietfLanguageTag))
                {
                    this._customDictionaryFiles[ietfLanguageTag] = new List <string>();
                }
                this._customDictionaryFiles[ietfLanguageTag].Add(text);
                using (new SpellerCOMActionTraceLogger(this, SpellerCOMActionTraceLogger.Actions.RegisterUserDictionary))
                {
                    SpellCheckerFactory.RegisterUserDictionary(text, ietfLanguageTag, true);
                }
                result = new Tuple <string, string>(ietfLanguageTag, text);
            }
            catch (Exception ex) when(ex is SecurityException || ex is ArgumentException || !flag)
            {
                if (text != null)
                {
                    FileHelper.DeleteTemporaryFile(text);
                }
                throw new ArgumentException(SR.Get("CustomDictionaryFailedToLoadDictionaryUri", new object[]
                {
                    lexiconFilePath
                }), ex);
            }
            return(result);
        }