48

My clipboard is populated with text, but when I run

string clipboardData = Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text);

I get back an empty string. I've toyed with various forms of the call including:

string clipboardData = Clipboard.GetText();
string clipboardData = Clipboard.GetText(System.Windows.Forms.TextDataFormat.UnicodeText);

But with the same result.

Am I missing something obvious?

3
  • How are you putting data in the clipboard? Feb 6, 2009 at 1:03
  • Is the data that is on the clipboard "text" and not graphic, audio or other?
    – JMD
    Feb 6, 2009 at 1:07
  • 1
    Null is very different from the empty string. I'm not sure why "null" is part of your question. It does nothing but add confusion and misinformation.
    – JoelFan
    Oct 2, 2020 at 18:59

6 Answers 6

62

You can only access the clipboard from an STA thread. Rick Brewster ran into this with some refactoring of the regular Edit->Paste command, in Paint.NET.

Code:

IDataObject idat = null;
Exception threadEx = null;
Thread staThread = new Thread(
    delegate ()
    {
        try
        {
            idat = Clipboard.GetDataObject();
        }

        catch (Exception ex) 
        {
            threadEx = ex;            
        }
    });
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
// at this point either you have clipboard data or an exception

Code is from Rick. http://forums.getpaint.net/index.php?/topic/13712-/page__view__findpost__p__226140

Update: Jason Heine made a good point of adding () after delegate to fix the ambiguous method error.

4
  • 5
    not sure about other frameworks, but in .net 4.0 delegate needs to have () after it, otherwise you get an ambiguous method error. Aug 9, 2011 at 17:08
  • Should we add some locking ?
    – Toto
    Dec 30, 2014 at 16:24
  • Good answer, although in my case it was just that I had a WinForms where I had removed the [STAThread] attribute on Main, so I just had to put it back. Jun 29, 2016 at 16:11
  • Hello, I find it that sometimes the clipboard content is not immediately available after staThread.Join() returns. e.g if I try to use idat I get an old value or null etc; However, if I add a 1 second sleep before trying to access the vairable it works. Any suggestions why this might be the case? Shall I just raise a new question? I think it is relevant here if the answer means we need a sleep before accessing the variable for a reliable read.
    – Ali Rad
    Jul 26, 2018 at 15:44
42

Honestly, I don't know what a STA thread is, but in simple projects it might solve the problem to add [STAThread] right before the Main method:

[STAThread]
static void Main(string[] args)
{ (...)

It works for me, so I don't question the method ;)


Further information about the [STAThread] decorator is on blog post Why is STAThread required?.

1
  • Perfect for a quick console app.
    – Grant
    Nov 16, 2013 at 21:57
11

I have written this class, it works, and do the same thing and can be easily improved just adding the method who you need

    Private Class ClipboardAsync

    Private _GetText As String
    Private Sub _thGetText(ByVal format As Object)
        Try
            If format Is Nothing Then
                _GetText = Clipboard.GetText()
            Else
                _GetText = Clipboard.GetText(DirectCast(format, TextDataFormat))
            End If

        Catch ex As Exception
            _GetText = String.Empty
        End Try
    End Sub
    Public Function GetText() As String
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thGetText)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start()
        staThread.Join()
        Return instance._GetText
    End Function
    Public Function GetText(ByVal format As TextDataFormat) As String
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thGetText)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start(format)
        staThread.Join()
        Return instance._GetText
    End Function

    Private _ContainsText As Boolean
    Private Sub _thContainsText(ByVal format As Object)
        Try
            If format Is Nothing Then
                _ContainsText = Clipboard.ContainsText()
            Else
                _ContainsText = Clipboard.ContainsText(DirectCast(format, TextDataFormat))
            End If
        Catch ex As Exception
            _ContainsText = False
        End Try
    End Sub
    Public Function ContainsText() As Boolean
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thContainsFileDropList)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start()
        staThread.Join()
        Return instance._ContainsText
    End Function
    Public Function ContainsText(ByVal format As Object) As Boolean
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thContainsFileDropList)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start(format)
        staThread.Join()
        Return instance._ContainsText
    End Function

    Private _ContainsFileDropList As Boolean
    Private Sub _thContainsFileDropList(ByVal format As Object)
        Try
            _ContainsFileDropList = Clipboard.ContainsFileDropList
        Catch ex As Exception
            _ContainsFileDropList = False
        End Try
    End Sub
    Public Function ContainsFileDropList() As Boolean
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thContainsFileDropList)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start()
        staThread.Join()
        Return instance._ContainsFileDropList
    End Function

    Private _GetFileDropList As Specialized.StringCollection
    Private Sub _thGetFileDropList()
        Try
            _GetFileDropList = Clipboard.GetFileDropList
        Catch ex As Exception
            _GetFileDropList = Nothing
        End Try
    End Sub
    Public Function GetFileDropList() As Specialized.StringCollection
        Dim instance As New ClipboardAsync
        Dim staThread As New Thread(AddressOf instance._thGetFileDropList)
        staThread.SetApartmentState(ApartmentState.STA)
        staThread.Start()
        staThread.Join()
        Return instance._GetFileDropList
    End Function
End Class

Here is the CSharp Version:

private class ClipboardAsync
{

private string _GetText;
private void _thGetText(object format)
{
    try {
        if (format == null) {
            _GetText = Clipboard.GetText();
        }
        else {
            _GetText = Clipboard.GetText((TextDataFormat)format);
            
        }
    }
    catch (Exception ex) {
        //Throw ex 
        _GetText = string.Empty;
    }
}
public string GetText()
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thGetText);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    return instance._GetText;
}
public string GetText(TextDataFormat format)
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thGetText);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start(format);
    staThread.Join();
    return instance._GetText;
}

private bool _ContainsText;
private void _thContainsText(object format)
{
    try {
        if (format == null) {
            _ContainsText = Clipboard.ContainsText();
        }
        else {
            _ContainsText = Clipboard.ContainsText((TextDataFormat)format);
        }
    }
    catch (Exception ex) {
        //Throw ex 
        _ContainsText = false;
    }
}
public bool ContainsText()
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thContainsFileDropList);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    return instance._ContainsText;
}
public bool ContainsText(object format)
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thContainsFileDropList);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start(format);
    staThread.Join();
    return instance._ContainsText;
}

private bool _ContainsFileDropList;
private void _thContainsFileDropList(object format)
{
    try {
        _ContainsFileDropList = Clipboard.ContainsFileDropList();
    }
    catch (Exception ex) {
        //Throw ex 
        _ContainsFileDropList = false;
    }
}
public bool ContainsFileDropList()
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thContainsFileDropList);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    return instance._ContainsFileDropList;
}

private Specialized.StringCollection _GetFileDropList;
private void _thGetFileDropList()
{
    try {
        _GetFileDropList = Clipboard.GetFileDropList();
    }
    catch (Exception ex) {
        //Throw ex 
        _GetFileDropList = null;
    }
}
public Specialized.StringCollection GetFileDropList()
{
    ClipboardAsync instance = new ClipboardAsync();
    Thread staThread = new Thread(instance._thGetFileDropList);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    return instance._GetFileDropList;
}
}

You can simple use it with: Vb.net:

Dim Clipboard2 As New ClipboardAsync
MessageBox.Show (Clipboard2.ContainsText())

Csharp:

ClipboardAsync Clipboard2 = new ClipboardAsync();
MessageBox.Show (Clipboard2.ContainsText());
7

BoltBait's code did not work for IDataObject because data object loses information outside the thread. Everything works fine, if the IDataObject is used only inside the thread like this:

IDataObject idat = null;
Exception threadEx = null;
String text = "";
Thread staThread = new Thread(
    delegate ()
    {
        try
        {
            idat = Clipboard.GetDataObject();
            text = idat.GetData(DataFormats.Text)
        }

        catch (Exception ex) 
        {
            threadEx = ex;            
        }
    });
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
// here you can use text, which contains data from clipboard
2

This is a threading problem. We have to get the right thread and execute through delegates.

I am updating my properties through a timer elapsing every 500 ms. Here is the code:

    public delegate void ClipboarDelegate();

    ClipboarDelegate clipboardDelegate = null;

    void clipboardTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (clipboardDelegate == null)
            clipboardDelegate = ClipboarDelegateMethod;

        //Here we get the right thread, most probably the application thread
        Application.Current.Dispatcher.BeginInvoke(clipboardDelegate);
    }

    public void ClipboarDelegateMethod()
    {
        try
        {
            if (Clipboard.ContainsData(DataFormats.Text))
            {
                //It's important to lock this section
                lock (ClipboardString)
                {
                    ClipboardString = Clipboard.GetData(DataFormats.Text) as string;
                }
            }
        }
        catch
        { }
    }

Moreover I've made a proper DependencyProperty with ClipboardString:

    public static readonly DependencyProperty ClipboardStringDP =
        DependencyProperty.Register("ClipboardString",
                                    typeof(string),
                                    typeof(MainWindow),
                                    new UIPropertyMetadata(string.Empty));

    public string ClipboardString
    {
        get { return (string)this.GetValue(ClipboardStringDP); }
        set { this.SetValue(ClipboardStringDP, value); }
    }

This way it can be bound to my TextBox in XAML assuming my control or window x:Name="_this":

<TextBox Name="ClipBoardTextBox"
         DataContext="{Binding ElementName=_this}"
         Text="{Binding Path=ClipboardString, Mode=OneWay}"/>
1
  • Is this meant as another answer to the original question? If so, then what does it offer that's new?
    – ClickRick
    Jun 15, 2014 at 17:24
0

For some reason BoltBait's code didn't quite work (idat was still null even after staThread.Join()). I just did Clipboard.GetText() inside of the staThread delegate instead of Clipboard.GetDataObject() and that worked fine.

Thanks though - your code snippet got me 99% there :)

1
  • Hi Jonathan - I saw the exact same thing and did the same thing as well.
    – Matthew
    Feb 10, 2009 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.