0

Hi guys I write a­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ console app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(String[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("No file to upload...");
                Environment.Exit(0);
            }
            else
                Console.WriteLine("[~] Trying to upload: " + args[0]);
            string name = Regex.Match(args[0], @"[^\\]*$").Value;
            ftp ftpClient = new ftp(@"ftp://site.ru/", "dfgd", "QWERTY_123");
            ftpClient.upload("www/site.ru/upload/" + name, args[0]);
            Console.WriteLine("[+] Upload File Complete");
            Console.ReadKey();
        }
    }
}

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ How after Console.WriteLine("[+] Upload File Complete"); copy args[0] to clipboard?

user1931796
  • 1
  • 1
  • 4
  • One possible solution is to use the [`Clipboard`](http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx) class in `System.Windows.Forms`, which you'd need to reference. – Adam Dec 29 '12 at 15:10
  • updated answer to avoid reference to **System.Windows.Forms** – Parimal Raj Dec 29 '12 at 15:19

2 Answers2

13

First you must add a reference to System.Windows.Forms in your application.

Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened.

You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.

using System;
using System.Windows.Forms;

class Program 
{
    [STAThread]
    static void Main(string[] args) 
    {
         Clipboard.SetText("this is in clipboard now");
    }
}

In case you dont want to use the reference to System.Windows.Forms, u can do it via P/Invoke

Platform Invoking the Clipboard APIs is a possible solution. Example:

using System.Runtime.InteropServices;
class Program
{
    [DllImport("user32.dll")]
    internal static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll")]
    internal static extern bool CloseClipboard();

    [DllImport("user32.dll")]
    internal static extern bool SetClipboardData(uint uFormat, IntPtr data);

    [STAThread]
    static void Main(string[] args)
    {
        OpenClipboard(IntPtr.Zero);
        var yourString = "Hello World!";
        var ptr = Marshal.StringToHGlobalUni(yourString);
        SetClipboardData(13, ptr);
        CloseClipboard();
        Marshal.FreeHGlobal(ptr);
    }
}

This is just an example. Adding a little error handling around the code, like checking the return values of the P/Invoke functions would be a good addition.

SetClipboardData is the interesting bit, you also want to make sure you open and close the clipboard, too.

The 13 passed in as the first argument is the data format. 13 means unicode string.

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • 1
    I read on MSDN "_If SetClipboardData succeeds, the system owns the object identified by the hMem parameter. The application may not write to or free the data_". But according to [these comments](http://stackoverflow.com/q/26054040/33499), `Marshal.StringToHGlobalUni` is unsuitable, see the other [answer](http://stackoverflow.com/a/24698804/33499) below. – wimh Mar 06 '16 at 10:44
6

The Marshal.StringToHGlobalUni function actually allocates memory in a fashion unsuitable for SetClipboardData (using LocalAlloc with LMEM_FIXED), which can cause crashes. (You wouldn't expect it given the method name, but stepping into the code e.g. using ReSharper reveals this.) SetClipboardData requires GlobalAlloc with GMEM_MOVABLE according to the docs: SetClipboardData on MSDN.

Here's an MIT licensed System.Windows.Forms alternative, tested and complete with error handling: Clippy

(the clipboard pushing code itself is to be found here: Clippy.cs

David
  • 261
  • 4
  • 4