Cleaning up preview files in a WPF application

Klicke hier für den deutschen Artikel.

Problem

When using the EnhancedViewerControl in a WPF application, the generated preview files (*.LL) are not automatically deleted from the temporary directory after the preview window is closed.

Solution

The EnhancedViewerControl provides access to the encapsulated preview control via its PreviewControl property. There, you can use the CloseMode property to configure the control so that the temporary preview file is automatically deleted when the control is closed.

Timing is important here: The CloseMode must be set before assigning the PreviewControl to List & Label:

enhancedViewerControl.PreviewControl.CloseMode =
    LlPreviewControlCloseMode.DeleteFile;

LL.PreviewControl = enhancedViewerControl.PreviewControl;

The .NET Help contains more information about the possible values for LlPreviewControlCloseMode.

Alternative solution using the API

If automatic deletion via CloseMode is not possible, the preview files can also be explicitly removed via the API.

First, set the temporary path:

private string _projectName;
private string _projectExt;
private string _previewPath = Path.GetTempPath() + "\\";

Before the print command is issued, the preview temp path is set:

_projectName = "Unterberichte und Relationen";
_projectExt = ".lsr";

string projectFile = _projectName + _projectExt;

LL.Core.LlPreviewSetTempPath(_previewPath);

After closing the window, the generated .LL file can be explicitly deleted:

private void Window_Closed(object sender, EventArgs e)
{
    enhancedPreviewControl.Dispose();

    string projectFile = _projectName + ".LL";
    LL.Core.LlPreviewDeleteFiles(projectFile, _previewPath);

    LL.Dispose();
}

The drawback of this approach is that the specific project name must be known or determined separately so that LlPreviewDeleteFiles can delete the corresponding preview files.