How to Copy Text to Clipboard in C#
We will learn in this post how to copy the contents of a string into the clipboard using the C# programming language.
All We Need to Know About Clipboard in C#
A user may transfer data from one area to another using a clipboard, a temporary storage space for the data. For instance, a user of a word processing tool may want to copy material from one portion of a document and paste it into another section of the document or in another place.
By using the methods made available by the Clipboard
class, you will be able to interact with the Clipboard
functionality built into the Windows operating system. Programmers and other users routinely save temporary data on the “Clipboard” in their applications.
For instance, word processors use the Clipboard whenever they execute actions involving cutting and pasting. A Clipboard is a tool that allows information to be transferred from one application to another.
The name given to the string used to identify the format is clipboard format.
When you hit Ctrl+C, some pieces of information or files are copied to the memory of the system’s Clipboard. There, they will stay until you press Ctrl+V to paste them back to where you copied them from the Clipboard.
The Clipboard
class allows users to copy and paste data and retrieve it from the Clipboard, as one of its many features.
Implementation of Copy Text to Clipboard in C#
In the following demonstration, we will copy all of the data from the Clipboard by using the Clipboard.GetDataObject()
command. The Clipboard may include pictures and other types of content that are not strings.
Therefore, we used the if else
conditions to verify whether or not it was in string format. If it was not, we inserted the error message or any other message required by our specifications.
IDataObject text = Clipboard.GetDataObject();
if (text.GetDataPresent(DataFormats.Text))
data.Text = (String)text.GetData(DataFormats.Text);
else
data.Text = "Data not found.";
There is still another option available for retrieving data from the clipboard. You are free to take advantage of the GetText
function available on the Clipboard.
To help you grasp it more thoroughly, here is an example.
data = Clipboard.GetText(TextDataFormat.text);
This is another method for retrieving the information saved in the Clipboard. You can also write the data into the Clipboard using the SetText
method available on the Clipboard.
Clipboard.SetText(data, TextDataFormat.text);
data
is the string that will be copied to the Clipboard, and TextDataFormat.text
is the format of the data supplied. If the data
is in HTML format, you must enter TextDataFormat.html
in the second part of the expression.
This article demonstrated how to copy the contents of a string into the Clipboard using the C# programming language.
I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.
LinkedIn