How to copy data to clipboard in C

How can I copy a string (such as "hello") to the system clipboard in C, so the next time I press CTRL + V, I get "hello"?

#1 building

My experience of Using WPF C to deal with this problem encountered by clipboard and System.Threading.ThreadStateException is here, my code can work normally on all browsers:

Thread thread = new Thread(() => Clipboard.SetText("String to be copied to clipboard"));
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start(); 
thread.Join();

Credit this article Ad locum

But this is only valid on the local host, so do not try on the server because it will not work properly.

On the server side, I use zero clipboard. After a lot of research, the only way.

#2 building

For step-by-step console projects, you must first add a System.Windows.Forms reference. The following steps work in the Visual Studio community 2013 with. NET 4.5:

  1. In solution explorer, expand your console project.
  2. Right click references, and then click Add Reference.
  3. Under the conference group framework, select System.Windows.Forms.
  4. Click OK.

Then, add the following using statement and other statements at the top of the code:

using System.Windows.Forms;

Then, add the following Clipboard . SetText Statement to add to your code:

Clipboard.SetText("hello");
// OR
Clipboard.SetText(helloString);

Finally, the following will STAThreadAttribute Add to your Main method to avoid System.Threading.ThreadStateException:

[STAThreadAttribute]
static void Main(string[] args)
{
  // ...
}

#3 building

You need a namespace declaration:

using System.Windows.Forms;

Or for WPF:

using System.Windows;

To copy the exact string (text in this case):

Clipboard.SetText("Hello, clipboard");

Copy the contents of the text box:

Clipboard.SetText(txtClipboard.Text);

See the example here . Or... MSDN official documents or WPF here .

#4 building

Clipboard.SetText("hello");

You will need to use the System.Windows.Forms or System.Windows namespace.

#5 building

Clip.exe is an executable file in Windows that is used to set up the clipboard. Note that this does not apply to other operating systems other than Windows, which is still bad.

        /// <summary>
        /// Sets clipboard to value.
        /// </summary>
        /// <param name="value">String to set the clipboard to.</param>
        public static void SetClipboard(string value)
        {
            if (value == null)
                throw new ArgumentNullException("Attempt to set clipboard with null");

            Process clipboardExecutable = new Process(); 
            clipboardExecutable.StartInfo = new ProcessStartInfo // Creates the process
            {
                RedirectStandardInput = true,
                FileName = @"clip", 
            };
            clipboardExecutable.Start();

            clipboardExecutable.StandardInput.Write(value); // CLIP uses STDIN as input.
            // When we are done writing all the string, close it so clip doesn't wait and get stuck
            clipboardExecutable.StandardInput.Close(); 

            return;
        }

Tags: Windows

Posted on Wed, 05 Feb 2020 00:55:04 -0500 by locomotive