2011-09-07 3 views

답변

2

응용 프로그램이 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; 
    } 
} 

희망,

+0

감사합니다 간주한다. 그것은 작동합니다! 실제로 Xfce를 사용하고 있으므로 잘 작동하는 것 같습니다. 그러나 ReceivedFunc에서 몇 가지 문제점을 발견했습니다. 이름에 공백이있는 파일을 좋아하지 않으므로 URL 인코딩을 제거하기 위해'System.Web.HttpUtility.UrlDecode' 호출을 추가했습니다. 또한 때때로 목록에 빈 문자열이있어서'if (items [i] == string.Empty) continue '를 for 루프의 시작 부분에 추가했습니다. 이 예제가 완벽하다는 것 외에도 완벽합니다. – MI3Guy