Home >Backend Development >C++ >How Can I Copy Data to the Clipboard in C#?
When working with text-based applications, it's often necessary to copy data to the clipboard so it can be pasted elsewhere. In C#, there are different mechanisms for copying data depending on the type of application.
In both Windows Forms and WPF applications, the Clipboard class can be used to access the system clipboard. To copy a specific string to the clipboard, use the SetText() method:
Clipboard.SetText("Hello, clipboard");
For console applications, it's necessary to add a reference to the System.Windows.Forms assembly. Use the following namespace declaration and ensure that the Main method is marked with the [STAThread] attribute:
using System.Windows.Forms; [STAThread] static void Main(string[] args) { Clipboard.SetText("Hello, clipboard"); }
To copy the contents of a textbox, either use the TextBox.Copy() method or retrieve the text and set the clipboard value:
Clipboard.SetText(txtClipboard.Text);
The above is the detailed content of How Can I Copy Data to the Clipboard in C#?. For more information, please follow other related articles on the PHP Chinese website!