-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWpfClipboard.cs
46 lines (37 loc) · 1.3 KB
/
WpfClipboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Windows;
namespace ScreenGrab {
public static class WpfClipboard {
public static IDataObject GetClipboardDataObject() {
for (int i = 0; i < 10; i++) {
try {
return Clipboard.GetDataObject();
} catch { }
System.Threading.Thread.Sleep(100);
}
//try one last time and allow exception to be thrown this time
return Clipboard.GetDataObject();
}
public static void SetClipboardDataObject(object data) {
for (int i = 0; i < 10; i++) {
try {
Clipboard.SetDataObject(data);
return;
} catch { }
System.Threading.Thread.Sleep(100);
}
//Try one last time and allow exception to be thrown this time.
Clipboard.SetDataObject(data);
}
public static void SetText(String value) {
SetClipboardDataObject(value);
}
public static string GetText() {
IDataObject dataObject = GetClipboardDataObject();
if (dataObject == null) {
return string.Empty;
}
return dataObject.GetData(DataFormats.Text) as string;
}
}
}