How to save clipboard contents

Is there a way to save the contents of the clipboard? I tried the following code, but it does not work.

Dim iData As IDataObject = Clipboard.GetDataObject()
...(use clipboard)
Clipboard.SetDataObject(iData)

Thanks.

+3
source share
4 answers

The easiest way to save the contents of the clipboard is to leave only the clipboard. The clipboard is intended as a temporary storage area for the user, not for applications, so the likelihood that you are trying to do has better decisions than cloning the clipboard.

+3
source

You can use OpenClipboard and CloseClipboard. According to MSDN, opening the clipboard will not allow other applications to modify data.

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool CloseClipboard();
0
source

? #, "CloseClipboard Failed ( HRESULT: 0x800401D4 (CLIPBRD_E_CANT_CLOSE))" Clipboard.SetDataObject(iData).

:

// save
Dictionary<String, Object> d = new Dictionary<String, Object>();
IDataObject ido = Clipboard.GetDataObject();
foreach (String s in ido.GetFormats(false))
    d.Add(s, ido.GetData(s));

// ...

// restore
var da = new DataObject();
foreach (String s in d.Keys)
    da.SetData(s, d[s]);
Clipboard.SetDataObject(da);
0

, . , ( MS Word). ( VSTO #):

                object start = 0;
                Word.Range startRng = a_TreatedDocument.Range(ref start, ref start);
                startRng.FormattedText = a_CoverPageDocument.Content.FormattedText;

. .

0

Source: https://habr.com/ru/post/1709342/


All Articles