Prevent 0x800401D3 CLIPBRD_E_BAD_DATA Error

1

Upon running the below code, every so often I can get an error

System.Runtime.InteropServices.COMException: Data on clipboard is invalid (Exception from HRESULT: 0x800401D3 (CLIPBRD_E_BAD_DATA))

I handle the error by the tried and true try-catch block, which is fine for my needs, but the app goes into a state where it either hard exits or the GUI becomes unresponsive and the app has to be killed from the task manager (I believe the hard exit is due to a main window inadvertantly being closed upon viewing the error).

The code checks for valid data ContainsData(DataFormats.Text) and if its ok, extracts the text and tries to determine if the text is a pathed file.

Is there a way to prevent the error in the first place? If there is no valid text, I do not need to do the process of checking the clipboard.

Code

try
{
     var doesClipboardHaveText = Clipboard.ContainsData(DataFormats.Text);

    if (doesClipboardHaveText)
    {
         var pathString = Clipboard.GetText(); // <- Exception Thrown here.

        if (pathString.IndexOfAny(System.IO.Path.GetInvalidPathChars()) == -1)
        {
            contains = pathString.Split(new char[] { ',' })
                              .Any(File.Exists);
        }
    }
}
c#
.net
x86
clipboard
asked on Stack Overflow Oct 5, 2020 by ΩmegaMan • edited Oct 5, 2020 by ΩmegaMan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0