Posted by: Cirilo Meggiolaro | 02/14/2009

Tip of the day #123 – Clipboard

You will agree that ctrl + c and ctrl + v are the most famous keyboard shortcuts. If they are not at least they are between the top 3. Let’s take a look today how to manipulate the clipboard from within our .NET applications.

Methods

The Clipboard class has several static methods that help us to save to and retrieve information from the clipboard. Let’s take a look on them:

  • void Clear(): Removes all data from the clipboard;
  • bool ContainsAudio(): Retrieves a Boolean flag that indicates whether there is data on the clipboard in the WaveAudio format or can be converted to that format;
  • bool ContainsData(string format): Retrieves a Boolean flag that indicates whether there is data on the clipboard that is in the specified format or can be converted to that format;
  • bool ContainsFileDropList(): Retrieves a Boolean flag that indicates whether there is data on the clipboard in the FileDrop format or can be converted to that format;
  • bool ContainsImage(): Retrieves a Boolean flag that indicates whether there is data on the clipboard in the Bitmap format or can be converted to that format;
  • bool ContainsText(): Retrieves a Boolean flag that indicates whether there is data on the clipboard in the Text or UnicodeText formats;
  • bool ContainsText(TextDataFormat format): Retrieves a Boolean flag that indicates whether there is data on the clipboard in the format specified by the TextDataFormat enumerator;
  • Stream GetAudioStream(): Retrieves an audio stream from the clipboard;
  • object GetData(string format): Retrieves an object from the clipboard on the specified format;
  • IDataObject GetDataObject(): Retrieves the data that is on the clipboard;
  • StringCollection GetFileDropList(): Retrieves a collection of file names from the clipboard;
  • Image GetImage(): Retrieves an image from the clipboard;
  • string GetText(): Retrieves text data from the clipboard in either Text or UnicodeText format;
  • string GetText(TextDataFormat format): Retrieves text data from the clipboard in the format indicated by the specified TextDataFormat;
  • void SetAudio(byte[] audioBytes): Adds a System.Byte array to the clipboard in the WaveAudio format after converting it to a System.IO.Stream;
  • void SetAudio(Stream audioStream): Adds a Stream to the clipboard in the WaveAudio format;
  • void SetData(string format, object data): Adds data to the clipboard in the specified format;
  • void SetDataObject(object data): Places nonpersistent data on the system clipboard;
  • void SetDataObject(object data, bool copy): Places data on the clipboard and specifies whether the data should remain on the clipboard after the application exits;
  • void SetDataObject(object data, bool copy, int retryTimes, int retryDelay): Attempts to place data on the clipboard the specified number of times and with the specified delay between attempts, optionally leaving the data on the clipboard after the application exits;
  • void SetFileDropList(StringCollection filePaths): Adds a collection of file names to the clipboard in the FileDrop format;
  • void SetImage(Image image): Adds an Image to the clipboard in the Bitmap format;
  • void SetText(string text): Adds text data to the clipboard in either Text or UnicodeText, depending on the operating system;
  • void SetText(string text, TextDataFormat format): Adds text data to the clipboard in the format indicated by the specified TextDataFormat parameter.

How to…

The following code snippets show how to set a block of text and how to get this text from the Clipboard:

/// Sets a block of text to the clipboard.
Clipboard.SetText(“This is a text copied to the Clipboard”);

/// Displays the text available in the clipboard.
Console.WriteLine(Clipboard.GetText());

/// Removes all text from the clipboard.
Clipboard.Clear();


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: