Add data to OS clipboard in ASP.Net/C#.Net

Some time we need to copy data into OS clipboard by code.

The Clipboard class provides methods that you can use to interact with the Windows operating system Clipboard feature. Many applications use the Clipboard as a temporary repository for data. For example, word processors use the Clipboard during cut-and-paste operations. The Clipboard is also useful for transferring data from one application to another.

 

 private static string strDataToAddInClipboard = string.empty;

 /// <summary>
 /// Method for copying the External data into the clipboard
 /// </summary>
 /// <remarks></remarks>
 protected static void CopyToClipBoard()
 {
     System.Windows.Forms.Clipboard.SetData(System.Windows.Forms.DataFormats.StringFormat,
     strDataToAddInClipboard);
 }

 [WebMethod()]
 [STAThread()]
 public static string BundleDocuments(string strExternalData)
 {
    try
    {
      if (!string.IsNullOrEmpty(strExternalData))
      {
        strDataToAddInClipboard = strExternalData;
        Thread myth = default(Thread);
        myth = new Thread(new System.Threading.ThreadStart(CopyToClipBoard));
        myth.SetApartmentState(ApartmentState.STA);
        myth.Start();
        return strExternalData;
     }
 }
 catch { }
}

References: http://msdn.microsoft.com/en-us/library/637ys738(v=vs.110).aspx

Programming is easy….