C# Gtk#剪贴板复制/剪切/粘贴文件

C# Gtk#剪贴板复制/剪切/粘贴文件,c#,clipboard,copy-paste,gtk#,C#,Clipboard,Copy Paste,Gtk#,如何使用Gtk将文件复制、剪切和粘贴到剪贴板。我所看到的每个示例都只显示文本被放入剪贴板。如果有人举一个例子,我将不胜感激。如果您的应用程序运行在Gnome/Unity上,您可以使用“x-special/Gnome copied files”目标类型在剪贴板上移动/复制文件。一旦文件被复制,您应该能够使用文件管理器粘贴它,反之亦然。下面是执行此操作的窗口类的示例: using System; using System.IO; using Gtk; public partial class Ma

如何使用Gtk将文件复制、剪切和粘贴到剪贴板。我所看到的每个示例都只显示文本被放入剪贴板。如果有人举一个例子,我将不胜感激。

如果您的应用程序运行在Gnome/Unity上,您可以使用“x-special/Gnome copied files”目标类型在剪贴板上移动/复制文件。一旦文件被复制,您应该能够使用文件管理器粘贴它,反之亦然。下面是执行此操作的窗口类的示例:

using System;
using System.IO;
using Gtk;

public partial class MainWindow : Gtk.Window
{
    private static Gdk.Atom _atom = Gdk.Atom.Intern("CLIPBOARD", false);
    private Gtk.Clipboard _clipBoard = Gtk.Clipboard.Get(_atom);
    private Gtk.FileChooserButton _fileCopy = null;
    private Gtk.FileChooserButton _folder = null;
    private Gtk.RadioButton _radioCopy = null;
    private Gtk.RadioButton _radioMove = null;
    private System.Text.ASCIIEncoding _encoding = new System.Text.ASCIIEncoding();
    private string _action = null;
    private string _source = null;
    private string _destination = null;

    public MainWindow () : base(Gtk.WindowType.Toplevel)
    {
        SetDefaultSize(200, -1);

        var table = new Gtk.Table(5, 5, true);
        var separator = new Gtk.HSeparator();

        var label0 = new Gtk.Label("Select file to copy/move");
        _fileCopy = new Gtk.FileChooserButton("Select A File", Gtk.FileChooserAction.Open);
        _radioCopy = new Gtk.RadioButton("Copy");
        _radioMove = new Gtk.RadioButton(_radioCopy, "Move");
        var copyButton = new Gtk.Button("Copy");

        Add(table);

        table.Attach(label0, 0, 4, 0, 1);
        table.Attach(_fileCopy, 0, 1, 1, 2);
        table.Attach(_radioCopy, 1, 2, 1, 2);
        table.Attach(_radioMove, 2, 3, 1, 2);
        table.Attach(copyButton, 3, 4, 1, 2);
        table.Attach(separator, 0, 4, 2, 3);

        var label1 = new Gtk.Label("Select destination for file(s)");
        _folder = new Gtk.FileChooserButton("Select A File", Gtk.FileChooserAction.SelectFolder);
        var pasteButton = new Gtk.Button("Paste");

        table.Attach(label1, 0, 4, 3, 4);
        table.Attach(_folder, 0, 1, 4, 5);
        table.Attach(pasteButton, 3, 4, 4, 5);

        DeleteEvent += OnDeleteEvent;
        copyButton.Clicked += OnCopyButtonClick; 
        pasteButton.Clicked += OnPasteButtonClick;

        ShowAll();
    }

    private void ClearGet(Gtk.Clipboard clipboard, Gtk.SelectionData selection, uint info)
    {
        var temp = _action + "\n" + _source;
        selection.Set(selection.Target, 8, _encoding.GetBytes(temp)); 
    }

    private void ClearFunc(Gtk.Clipboard clipboard)
    {
        //???
    }

    private void ReceivedFunc(Gtk.Clipboard clipboard, Gtk.SelectionData selection)
    {
        var temp = _encoding.GetString(selection.Data);
        if (temp==null) return;

        var items = temp.Split();
        for (int i=1; i<items.Length; i++)
        {
            var fileFrom = items[i].Substring("file://".Length);
            var fileTo = System.IO.Path.Combine(_destination, System.IO.Path.GetFileName(fileFrom));
            if (items[0]=="copy")
                File.Copy(fileFrom, fileTo);
            else if (items[1]=="cut")
                File.Move(fileFrom, fileTo);
        }
    }

    private void OnCopyButtonClick(object obj, EventArgs args)
    {
        Console.WriteLine(_fileCopy.Uri);

        _source = _fileCopy.Uri;
        _action = _radioMove.Active ? "cut" : "copy";

        var target0 = new TargetEntry("x-special/gnome-copied-files", 0, 0);
        var target1 = new TargetEntry("text/uri-list", 0, 0);

        _clipBoard.SetWithData(new TargetEntry[] {target0, target1}, ClearGet, ClearFunc);
    }

    private void OnPasteButtonClick(object obj, EventArgs args)
    {
        _destination = _folder.Filename;
        _clipBoard.RequestContents(Gdk.Atom.Intern("x-special/gnome-copied-files", false), ReceivedFunc);
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}
使用系统;
使用System.IO;
使用Gtk;
公共部分类主窗口:Gtk.Window
{
私有静态Gdk.Atom _Atom=Gdk.Atom.Intern(“剪贴板”,false);
private Gtk.Clipboard _Clipboard=Gtk.Clipboard.Get(_atom);
private Gtk.FileChooserButton_fileCopy=null;
private Gtk.FileChooserButton_folder=null;
专用Gtk.RadioButton _radioCopy=null;
专用Gtk.RadioButton _radioMove=null;
private System.Text.asciencoding_encoding=新系统.Text.asciencoding();
私有字符串_action=null;
私有字符串_source=null;
私有字符串_destination=null;
公共主窗口():基本(Gtk.WindowType.Toplevel)
{
SetDefaultSize(200,-1);
var表=新的Gtk表(5,5,真);
var separator=new Gtk.hsepator();
var label0=new Gtk.Label(“选择要复制/移动的文件”);
_fileCopy=new Gtk.FileChooserButton(“选择一个文件”,Gtk.FileChooserAction.Open);
_radioCopy=新Gtk.RadioButton(“复制”);
_radioMove=新的Gtk.无线电按钮(_radioCopy,“Move”);
var copyButton=新的Gtk.按钮(“复制”);
增加(表);
附表(标签0,0,4,0,1);
表.附件(_fileCopy,0,1,1,2);
表.附上(_放射检查,1,2,1,2);
表.附件(_radioMove,2,3,1,2);
表.附件(复印按钮,3,4,1,2);
表.附件(分隔符,0,4,2,3);
var label1=new Gtk.Label(“为文件选择目标”);
_folder=new Gtk.FileChooserButton(“选择文件”,Gtk.FileChooserAction.SelectFolder);
var pasteButton=新的Gtk.按钮(“粘贴”);
附表(标签1,0,4,3,4);
表.附加(_文件夹,0,1,4,5);
表.附件(粘贴按钮,3,4,4,5);
DeleteEvent+=OnDeleteEvent;
copyButton.Clicked+=oncopyButton单击;
粘贴按钮。单击+=打开粘贴按钮单击;
ShowAll();
}
私有void ClearGet(Gtk.Clipboard剪贴板、Gtk.SelectionData selection、uint info)
{
var temp=_action+“\n”+_source;
selection.Set(selection.Target,8,_encoding.GetBytes(temp));
}
私有void ClearFunc(Gtk.Clipboard剪贴板)
{
//???
}
私有void ReceivedFunc(Gtk.Clipboard剪贴板,Gtk.SelectionData selection)
{
var temp=_encoding.GetString(selection.Data);
如果(temp==null)返回;
var items=temp.Split();

对于(int i=1;我感谢你。它很有效!我实际上使用的是Xfce,因此它似乎也很有效。但是,我发现ReceivedFunc存在一些问题。它不喜欢名称中带有空格的文件,因此我添加了一个
System.Web.HttpUtility.UrlDecode
调用来删除url编码。此外,列表中有时还有空白字符串,因此我添加了
if(items[i]==string.Empty)继续;
到for循环的开头。除此之外,此示例非常完美。