Подтвердить что ты не робот

Clipboard.GetText возвращает null (пустая строка)

Мой буфер обмена заполнен текстом, но когда я запускаю

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

Я возвращаю пустую строку. Я играл с различными формами вызова, включая:

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

Но с тем же результатом.

Я пропустил что-то очевидное?

4b9b3361

Ответ 1

Доступ к буфере обмена возможен только из потока STA. Рик Брюстер столкнулся с этим с некоторым рефакторингом обычной команды Edit- > Paste в Paint.NET.

код:

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

Код от Рика. http://forums.getpaint.net/index.php?/topic/13712-/page__view__findpost__p__226140

Обновить: Джейсон Хейн дал хорошую возможность добавить () после delegate, чтобы исправить ошибку неоднозначного метода.

Ответ 2

Честно говоря, я не знаю, что такое поток STA, но в простых проектах может решить проблему добавить [STAThread] прямо перед методом Main:

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

Это работает для меня, поэтому я не стану подвергать сомнению метод;)


Дополнительная информация о декораторе [STAThread] находится в сообщении блога Почему требуется STAThread?.

Ответ 3

Я написал этот класс, он работает и делает то же самое, и его можно легко улучшить, просто добавив метод, который вам нужен.

    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

Вот версия CSharp:

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;
}
}

Вы можете просто использовать его с: Vb.net:

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

Csharp:

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

Ответ 4

Код BoltBait не работал для IDataObject, потому что объект данных теряет информацию за пределами потока. Все работает нормально, если IDataObject используется только внутри потока, например:

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

Ответ 5

Это проблема с потоками. Мы должны получить нужный поток и выполнить через делегатов.

Я обновляю свои свойства через таймер, истекающий каждые 500 мс. Вот код:

    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 important to lock this section
                lock (ClipboardString)
                {
                    ClipboardString = Clipboard.GetData(DataFormats.Text) as string;
                }
            }
        }
        catch
        { }
    }

Кроме того, я создал правильный DependencyProperty с 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); }
    }

Таким образом, он может быть привязан к моему TextBox в XAML, предполагая мой элемент управления или окно x:Name="_this":

<TextBox Name="ClipBoardTextBox"
         DataContext="{Binding ElementName=_this}"
         Text="{Binding Path=ClipboardString, Mode=OneWay}"/>

Ответ 6

По какой-то причине код BoltBait не совсем работает (idat все еще был нулевым даже после staThread.Join()). Я просто сделал Clipboard.GetText() внутри делегата staThread вместо Clipboard.GetDataObject(), и это отлично работало.

Спасибо, хотя - ваш фрагмент кода получил меня на 99%:)