@bobnoordam

Getting and setting the clipboard content

These samples show handling the clipboard in the most basic way. First a test is done to see if there currently is text on the clipboard. If a text is found, the programm proceeds to read and display the text, and then replace it with a new text. By modifieing the parameters you can use the same technique to handle other formats such as images or audio and video fragments.

VB.NET Implementation

Imports System.Windows.forms
  
Module Module1
  
    Sub Main()
  
        If Clipboard.ContainsText = True Then
  
            ' --- Get the current text from the clipboard
            Dim sClipboard As String = Clipboard.GetText
            Console.WriteLine("The text on the clipboard was: " & sClipboard)
            ' //
  
            ' --- Set a new text on the clipboard
            Dim sNewValue As String = "This is now on the clipboard"
            Clipboard.SetText(sClipboard)
            Console.WriteLine("The new value is : " & Clipboard.GetText)
            ' //
  
        Else
            Console.WriteLine("Sorry, the clipboard does not contain a text, the demo failed")
        End If       
  
    End Sub
  
End Module

C# Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;  // dont forget to add this one
  
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
  
            if (Clipboard.ContainsText() == true)
            {
                // Get and display the current clipboard text
                string sCurrent = Clipboard.GetText();
                Console.WriteLine("The text on the clipboard was: " + sCurrent);
  
                // Set a new value onto the clipboard, and display it to the user
                string sNew = "This is now on the clipboard";
                Clipboard.SetText(sNew);
                Console.WriteLine("The new value on the clipboard is: " + Clipboard.GetText());
                      
            }
            else
            {
                Console.WriteLine("Sorry, the clipboard does not contain a text, the demo failed");
            }
  
        }
    }
}