2012-04-24 35 views
-1

ı發送我的文件到cliboard和ı想要複製指定的所有文件的路徑例如一個按鈕單擊桌面,文件如但我有問題我不會從列表框中獲取所有文件和我牛逼副本指定的路徑我怎麼能複製所有文件....c#將文件複製到指定路徑

public partial class Form1 : Form 
{ 
    [DllImport("User32.dll", CharSet = CharSet.Auto)] 
    public static extern IntPtr SetClipboardViewer(IntPtr hWnd); 
    [DllImport("User32.dll", CharSet = CharSet.Auto)] 
    public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext); 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); 

    IntPtr SonrakiClipboardOgesi; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     SonrakiClipboardOgesi = SetClipboardViewer(this.Handle); 
    } 

    protected override void WndProc(ref Message m) 
    { 
     int WM_DRAWCLIPBOARD = 0x0308; 
     int WM_CHANGECBCHAIN = 0x030D; 

     if (m.Msg == WM_DRAWCLIPBOARD) 
     { 
      ClipboardRead(); 
      SendMessage(SonrakiClipboardOgesi, m.Msg, m.WParam, m.LParam); 
     } 
     else if (m.Msg == WM_CHANGECBCHAIN) 
     { 
      if (m.WParam == SonrakiClipboardOgesi) 
      { 
       SonrakiClipboardOgesi = m.LParam; 
      } 
      else 
      { 
       SendMessage(SonrakiClipboardOgesi, m.Msg, m.WParam, m.LParam); 
      } 
     } 

     base.WndProc(ref m); 
    } 

    private void ClipboardRead() 
    { 
     StringCollection col = new StringCollection(); 
     col = Clipboard.GetFileDropList(); 
     for (int i = 0; i < col.Count; i++) 
     { 
      listBox1.Items.Add(col[i]); 
     } 
     listBox1.SelectionMode = SelectionMode.MultiSimple; 
     for (int i = 0; i < listBox1.Items.Count; i++) 
     { 
      listBox1.SetSelected(i, true); 
     } 
    } 

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     ChangeClipboardChain(this.Handle, SonrakiClipboardOgesi); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // ı make a with click copy within listbox files specified path 
     //What code I should write here 
    } 
} 

回答

0

你所試圖做的是有點多餘的,因爲你並不需要,如果你有一個按鈕單擊事件收聽到剪貼板。 ..

if (Clipboard.ContainsFileDropList()) 
{ 
    // Get a list of files 
    System.Collections.Specialized.StringCollection returnList = Clipboard.GetFileDropList(); 
    // For each file in the list 
    foreach(string s in returnlist) 
    { 
     // split the file path and get the last node of the path which should be file.ext 
     String[] sa = s.Split('\'); 
     string sourceFile = s; 
     // set the file target 
     string targetFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop))+sa[sa.length-1]; 
     // Get a list of files 
     System.IO.File.Copy(sourceFile, destFile, true); // finally copy the file 
    } 
} 

您可能需要做一些調試,因爲我沒有Visual Studio h安迪和我沒有檢查,如果它編譯...

+0

這是代碼的錯誤,我讓你知道我會告訴你從土耳其列表框中 – leonf 2012-04-24 10:11:25

+0

的所有文件,你說土耳其語sevki? – leonf 2012-04-24 10:31:06

+0

私人無效的button1_Click(對象發件人,EventArgs的){ // 我點擊列表框指定文件中拷貝路徑做出 //我應該在這裏寫 什麼碼} – leonf 2012-04-25 10:15:41

0

你已經問過這個問題,但我無法找到任何地方。 反正這裏是代碼如何從列表框中的文件複製到桌面:

foreach (string item in listBox1.Items) 
{ 
    FileInfo fileInfo = new FileInfo(item); 
    File.Copy(item,Path.Combine(
      Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
      fileInfo.Name), true); 
} 
+0

這段代碼拋出錯誤(文件''已經存在)。 – leonf 2012-04-24 10:10:08

+0

當然,如果你已經在桌面上有相同的文件,它會拋出異常。要覆蓋現有文件,您需要設置第三個File.Copy方法參數 - 請參閱編輯的代碼。 – Reniuz 2012-04-24 10:16:01

+0

@leonf瞭解它是如何工作的,然後對其進行調試以便它適用於您,不要期望複製粘貼代碼的答案。 – Amicable 2012-04-24 12:07:04

相關問題