使用文本框和自定义词典进行拼写会减慢我在 C# WPF 中的应用程序

Spelling with textboxes and custom dictionaries slowing down my application in C# WPF

我在 WinForm 应用程序中使用 WPF TextBoxes 进行拼写检查。每次我创建一个时,我都会将相同的文件加载为 CustomDictionary。直到最近一切都很好。现在,它们需要很长时间才能加载,最多一秒钟。有些表格有 30 个或更多,这意味着将近半分钟的延迟。这似乎是 Windows 10 的情况(不是我最初发布的 Windows 8)。应用程序是运行在DotNet 4.0下,我试过4.5和4.6(不是4.61),所有版本都很慢。

看到了sfaust的问题和am7zd的回答。多亏了这些,我查看了 HKEY_CURRENT_USER\Software\Microsoft\Spelling\Dictionaries 中的 GLOBAL 注册表项。我有 580 个条目(在删除没有匹配文件的条目之后)仍然很慢。

目前,每次我创建一个TextBox并向其中添加一个自定义词典时,似乎都会在_GLOBAL_中生成一个新条目

非常感谢收到任何建议。

没有其他人的回答,所以这就是我所做的:

  1. 我确保在关闭它们所在的表单之前对所有带有自定义词典的文本框使用 CustomDictionaries.Remove。这会删除 _GLOBAL_ 中的新条目和 AppData\Local\Temp.
  2. 中的相关文件

但有时会出现问题,或者用户只是结束任务,留下 _GLOBAL_ 条目和 .dic 文件,所以:

  1. 我决定更进一步。当我启动我的应用程序时,我不仅会清除 _GLOBAL_ 中没有匹配文件的条目(如上文提到的 post 中所建议),而且还会删除 [=] 中引用 .dic 文件的所有条目29=]。我的理论是,任何在此处留下条目的人都不是故意的,否则他们可能会将 .dic 文件保存在不同的文件夹中(就像 Microsoft Office 所做的那样)。

        try
        {
    
            string[] allDictionaries = (string[])Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Spelling\Dictionaries", "_Global_",  new string[0]);
    
            if (allDictionaries.Count() > 0)
            {
                List<string> realDictionaries = new List<string>();
                bool changedSomething = false;
    
                foreach (string thisD in allDictionaries)
                {
                    if (File.Exists(thisD))
                    {
                        if (thisD.Contains(@"\AppData\Local\Temp\"))
                        {
                            // Assuming that anyone who wants to keep a permanent .dic file will not store it in \AppData\Local\Temp
                            // So delete the file and don't copy the name of the dictionary into the list of good dictionaries.
                            File.Delete(thisD);
                            changedSomething = true;
                        }
                        else
                        {
                            realDictionaries.Add(thisD);
                        }
                    }
                    else
                    {
                        // File does not exist, so don't copy the name of the dictionary into the list of good dictionaries.
                        changedSomething = true;
                    }
                }
    
                if (changedSomething)
                {
                    Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Spelling\Dictionaries", "_Global_", realDictionaries.ToArray());
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, "Error clearing up old dictionary files.\n\nFull message:\n\n" + ex.Message, "Unable to delete file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    

我仍然想知道清除 _GLOBAL_ 中引用 AppData\Local\Temp 中文件的条目是否完全安全。人们当然不应该将重要的东西留在临时文件夹中......应该吗?

真正好的是 CustomDictionaries.Add 的重载,它允许我们设置 .dic 文件的名称和文件夹,允许同一应用程序中的所有文本框共享同一个 .dic 文件并确保我们不会在一开始就留下大量看似随机名称的冗余条目和文件......请微软。