Wes
Wes

Reputation: 1945

How do I get a list of all data from clipboard history in C#?

I want to get a list of all data from the clipboard history but I can't find an enumerator method. Would there be something I'm missing or what other way can I do it? I can't find an enumerator method in the clipboard class.

var clip = Clipboard.GetDataObject();

foreach (var item in clip)
{
    MessageBox.Show(item);
}

Upvotes: 6

Views: 3116

Answers (1)

Wes
Wes

Reputation: 1945

I was able to get clipboard history by referencing the Clipboard class from WinRT API onto my WPF application.

using Clipboard = Windows.ApplicationModel.DataTransfer.Clipboard;

Task.Run(async () => {
    var items = await Clipboard.GetHistoryItemsAsync();
    foreach (var item in items.Items)
    {
        string data = await item.Content.GetTextAsync();
        MessageBox.Show(data);
    }
});

I also had to set my target framework to .NET 5.0 with a TFM version and didn't need any NuGet packages for this to work. You will need the Microsoft.Windows.SDK.Contracts NuGet Package on earlier versions of .NET.

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>

Upvotes: 10

Related Questions