Copying Data to the System Clipboard in C#
In C#, accessing the system clipboard requires selecting the appropriate namespace based on the application type and ensuring the Main function (if applicable) is attributed with [STAThread]. Here's a breakdown:
To copy a literal string to the clipboard:
Clipboard.SetText("Hello, clipboard");
For copying the contents of a textbox, either use TextBox.Copy() or:
Clipboard.SetText(txtClipboard.Text);
Note that the clipboard is a desktop UI concept, so accessing it from server-side code (e.g., ASP.Net) will only affect values on the server and not impact the user's browser.
Additionally, ensure that the current thread is set to a single thread apartment (STA) to avoid exceptions related to Clipboard access.
The above is the detailed content of How Do I Copy Data to the System Clipboard in C#?. For more information, please follow other related articles on the PHP Chinese website!