How do I copy the contents of a String to the clipboard in C#?
You can use System.Windows.Forms.Clipboard.SetText(...)
.
How to copy data to clipboard in C#
There are two classes that lives in different assemblies and different namespaces.
WinForms: use following namespace declaration, make sure
Main
is marked with[STAThread]
attribute:using System.Windows.Forms;
WPF: use following namespace declaration
using System.Windows;
console: add reference to
System.Windows.Forms
, use following namespace declaration, make sureMain
is marked with[STAThread]
attribute. Step-by-step guide in another answerusing System.Windows.Forms;
To copy an exact string (literal in this case):
Clipboard.SetText("Hello, clipboard");
To copy the contents of a textbox either use TextBox.Copy() or get text first and then set clipboard value:
Clipboard.SetText(txtClipboard.Text);
See here for an example.
Or... Official MSDN documentation or Here for WPF.
Remarks:
Clipboard is desktop UI concept, trying to set it in server side code like ASP.Net will only set value on the server and has no impact on what user can see in they browser. While linked answer lets one to run Clipboard access code server side with
SetApartmentState
it is unlikely what you want to achieve.If after following information in this question code still gets an exception see "Current thread must be set to single thread apartment (STA)" error in copy string to clipboard
This question/answer covers regular .NET, for .NET Core see - .Net Core - copy to clipboard?
copy text in clipboard
You should use the Clipboard
class (msdn) and SelectedText
property of TextBox
(msdn):
To copy data to the clipboard use the SetText
method (msdn).
To get data from the clipboard use the GetText
method (msdn).
The following code allows you to copy selected text from TextBox
to clipboard:
Clipboard.SetText(tbText.SelectedText);
How to get content of a text file and copy it to clipboard?
You need a reference to System.Windows
or System.Windows.Forms
var content = File.ReadAllText("filepath.txt");
Clipboard.SetText(content);
How to copy data in a text file to the clipboard?
if you want to copy the content of a text file to Clipboard
the use the following code:
Clipboard.SetText(File.ReadAllText("your file path"));
(Original answer, before question edit: If you want to copy the content of a notepad to Clipboard
, then just select the text and press ctrl+c
. it will copy the text to the Clipboard
.)
How do I copy text to the clipboard with partial italics?
It seems you have to insert a 'header' in the html string, I found 2 examples for this:
- MSDN blog
- StackOverFlow
This works with Word:
Example code:
Clipboard.SetText(@"Version:1.0
StartHTML:000125
EndHTML:000260
StartFragment:000209
EndFragment:000222
<HTML>
<head>
<title>HTML clipboard</title>
</head>
<body>
<!–StartFragment–><b>Hello!</b><!–EndFragment–>
</body>
</html>",
TextDataFormat.Html);
This copies a Hello! to your clipboard, you have to change the fragments based on the size I think so I don't know exactly how that would work with a dynamic string but I hope this will get you started. 666
If you can also use RTF
Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 italic\i0.}",
TextDataFormat.Rtf);
Example with string
var q = "test123";
Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 " + q + @"\i0.}",
TextDataFormat.Rtf);
or
var q = "test123";
Clipboard.SetText( $@"{\rtf1\ansi This is in \i\f0\fs17 {q}\i0.}",
TextDataFormat.Rtf);
Note the @
before the 2nd part of the string, if you need to escape certain characters (you will need that with RTF) add @
in front of each opening "
.
This seems to be a lot easier because you don't have to insert the header but the formatting itself is abit more complicated imo.
Related Topics
How to Perform an Insert and Return Inserted Identity with Dapper
Where to Find "Microsoft.Visualstudio.Testtools.Unittesting" Missing Dll
Mvvm: Binding to Model While Keeping Model in Sync with a Server Version
How to Import from Excel to a Dataset Using Microsoft.Office.Interop.Excel
Convert Newtonsoft.JSON.Linq.Jarray to a List of Specific Object Type
How to Get a Count of the Total Number of Digits in a Number
Add a Package with a Local Package File in 'Dotnet'
Problems Using Entity Framework 6 and SQLite
How to Store JSON in an Entity Field with Ef Core
Return Content with Ihttpactionresult for Non-Ok Response
Remove the Title Bar in Windows Forms
Create Web Service Proxy in Visual Studio from a Wsdl File
Casting Object to Int Throws Invalidcastexception in C#
How to Make a Wizard with ASP.NET MVC
How to Determine a Session Variable Is Null or Empty in C#
The Application Called an Interface That Was Marshalled for a Different Thread - Windows Store App