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?
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 the contents of a String to the clipboard in C#?
You can use System.Windows.Forms.Clipboard.SetText(...)
.
How to copy data to clipboard when user selects Copy from ContextMenu
I guess what I'd do is this:
var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type != DataGridViewHitTestType.Cell) { return; }
var mi = new MenuItem("Copy")
mi.Tag = hitTestInfo;
mi.Click += (s, e) =>
{
var hti = ((MenuItem)s).Tag as HitTestInfo;
var val = dataGridView1.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Value;
Clipboard.SetData(DataFormats.Text, val);
}
m.MenuItems.Add(mi);
How to copy multiple items to clipboard history in c#?
It takes some delays for Clipboard history to save the current item. Therefore, you could try to add a delay when an item is added.
Please check the following code as a sample:
private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if(Clipboard.IsHistoryEnabled())
{
List<string> lists=new List<string>{ "1","2","3","4","5","6","7","8","9","10"};
foreach(var item in lists)
{
DataPackage dataPackage = new DataPackage();
dataPackage.SetText(item);
Clipboard.SetContent(dataPackage);
await Task.Delay(250);
}
}
}
Note, if these items are not all added, you could increase the delay time.
Related Topics
How to Remove Whitespace on Merge
Adding Stylesheets Programmatically in ASP.NET
Catching "Maximum Request Length Exceeded"
Unity Singleton Manager Classes
ASP.NET MVC 5 - Identity. How to Get Current Applicationuser
Showing Difference Between Two Datetime Values in Hours
Tuples( or Arrays ) as Dictionary Keys in C#
When Is It Appropriate to Use C# Partial Classes
Initializing C# Auto-Properties
How to Cast from Parent Class to Child Class
SQL Command Insert Is Working But the Data Not Appear in Table
C# (Mono) Linux Web Server Hosting with Consistent Static Variables Across Threads
How to Read an Entire File to a String Using C#
How to Connect to Database from Unity
When Should I Use Async Controllers in ASP.NET MVC
How to Stop Entity Framework from Trying to Save/Insert Child Objects