4

I'm having a problem whereby '.dic' files are being created in %TEMP% and not removed when i add custom dictionaries to textboxes/richtextboxes. This worked fine in Windows 7 but now on Win 10 we are seeing the above problem. The files are always zero bytes and are named something like 1s3iqqvw.dic.

The real app has plenty of text/richtextboxes and each time one is instantiated a new dic file gets created when the custom dictionary is added, users soon end up with hundreds of these files in their temp directories.

Whats worse is the process gets slower and slower as more .dic files get created - Windows appears to be generating the random filename then searching the directory for a duplicate before it creates it, repeating until it finds a unique one. This slows down the app considerably when there are hundreds on .dic files - we need to delete the .dic manually to fix.

the following code will recreate the issue. On Win 10 .dic files are created in %TEMP% - this is not the case on Win 7

{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //dict.txt must exist
            this.textBox1.SpellCheck.CustomDictionaries.Add(new Uri("d:\\dict.txt", UriKind.Relative));
            textBox1.Text = "Lorem ipsum dolor sit amet";
        }
    }
}

<Window x:Class="testapp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:u="clr-namespace:testapp"
        Title="MainWindow" Height="350" Width="799"
        >
    <Grid>
        <TextBox SpellCheck.IsEnabled="True" Height="287" Margin="6,12,0,0" Name="textBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="759" TextWrapping="Wrap" AcceptsReturn="True"/>
    </Grid>
</Window>

Is anyone aware of this issue on Windows 10? Do you have a fix?

1 Answers1

0

I know this is not a real fix for the issue, but at the moment it seems to at least help. I could not find a way to avoid creating the .dic files. However, looking into the code I found that clearing the CustomDictionaries will trigger the .dic file to be deleted.

My idea at the moment is to clear them when the window is closed. You can either clear them each manually, or loop through them similar to below. (Thanks to the post Find all controls in WPF Window by type)

    public MainWindow()
    {
        InitializeComponent();

        this.textBox1.SpellCheck.CustomDictionaries.Add(new Uri("C:\\dict.txt", UriKind.Relative));
        textBox1.Text = "Lorem ipsum dolor sit amet";

        this.Closed += ((object sender, EventArgs e) => {
            foreach (var txtbox in FindVisualChildren<TextBox>(this))
            {
                txtbox?.SpellCheck?.CustomDictionaries?.Clear();
            }
        });
    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

In case you are interested, the source code that I believe is creating the issue is available here: (https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Documents/WinRTSpellerInterop.cs,2777e92ca38df44a,references)

Wiz
  • 406
  • 3
  • 7