结果复制到剪贴板 [英] Copy result to clipboard
问题描述
大家好我写的a控制台应用程序:
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();
}
}
}
如何在 Console.WriteLine([+]上传文件的完整);
复制 ARGS [0]
到剪贴板?
How after Console.WriteLine("[+] Upload File Complete");
copy args[0]
to clipboard?
推荐答案
首先,你必须将引用添加到您的应用程序的 System.Windows.Forms的。
First you must add a reference to System.Windows.Forms in your application.
转到项目 - >从.NET选项卡添加在刚打开窗户参考,选择System.Windows.Forms的。
Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened.
您必须通过应用STAThread属性为您的main()函数避免ThreadStateException。然后你可以使用剪贴板功能,没有任何问题。
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");
}
}
如果你不想使用中提及的 System.Windows.Forms的,U可以通过做的P / Invoke
平台调用的API剪贴板是一个可能的解决方案。例如:
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);
}
}
这仅仅是一个例子。加入少许错误处理周围的code,如检查P的返回值/启动功能将是一个很好的补充。
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 code>
是有趣的一点,你也想确保你打开和关闭剪贴板了。
SetClipboardData
is the interesting bit, you also want to make sure you open and close the clipboard, too.
的 13
传递的第一个参数是数据格式。 13 意味着单向code字符串。
The 13
passed in as the first argument is the data format. 13 means unicode string.
这篇关于结果复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!