<p>Why does the following code sometimes causes an Exception with the contents "CLIPBRD_E_CANT_OPEN":</p>

<pre><code>Clipboard.SetText(str);
</code></pre>

<p>This usually occurs the first time the Clipboard is used in the application and not after that.</p>

Actually, I think this is the
fault of the Win32 API
.
To set data in the clipboard, you have to
open it
first.  Only one process can have the clipboard open at a time.  So, when you check, if another process has the clipboard open
for any reason
, your attempt to open it will fail.
It just so happens that Terminal Services keeps track of the clipboard, and on older versions of Windows (pre-Vista), you have to open the clipboard to see what's inside... which ends up blocking you.  The only solution is to wait until Terminal Services closes the clipboard and try again.
It's important to realize that this is not specific to Terminal Services, though: it can happen with anything.  Working with the clipboard in Win32 is a giant race condition.  But, since by design you're only supposed to muck around with the clipboard in response to user input, this usually doesn't present a problem.