Clipboard Copying objects to and from

前端 未结 5 1215
说谎
说谎 2020-12-01 21:09

I am trying to copy an object onto the windows clipboard and off again. My code is like this:

Copy on to clipboard:

Clipboard.Clear();
DataObject ne         


        
5条回答
  •  孤城傲影
    2020-12-01 21:55

    I came across this while trying to send a list of items to my clipboard. What I needed was the string representation of those items, not the entire object. I tried a few of the suggestions here, to no avail. However, I did come up with my own solution and I wanted to share it. See below.

    public static void CopyToClipboard(this IEnumerable items)
    {
        StringBuilder stringBuilder = new StringBuilder();
    
        foreach (T item in items)
            stringBuilder.Append(item.ToString()).AppendLine();
    
        Clipboard.SetText(stringBuilder.ToString());
    }
    

    Add this as an extension method and be sure to override your custom Type's ToString() method.

提交回复
热议问题