سؤال

I'd like to populate the Forms.Clipboard with text from a NUnit test.

The first problem I encountered was that the Clipboard must be used in STA mode. I found the solution (NUnit 2.5.x+) to set the RequiresSTA attribute on the method.

Next I get an ExternalException when calling Clipboard.SetText(). I found the solution to use Clipboard.SetDataObject(object, bool, int, int) where you can specify retryTimes to make several attempts to set the clipboard. This hacky solution works sometimes, which obviously isn't good enough (increasing the retryTimes parameter will of course also increase the chance of succeeding).

Others (for instance Peter) have succeeded with this, so perhaps it has something to do with my environment: I run VS2010 under Windows 7 64-bit targeting .NET 3.5 and x86. The NUnit version is 2.5.9.

I'm stuck. Any help would be appreciated.

هل كانت مفيدة؟

المحلول

Answering my own question:

As suggested by Hans Passant in the comments, the problem was that another process was interfering. The evil process seems to be Google Chrome.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top