2012-04-24 2 views
-1

ı 내 파일을 cliboard에 보내고 ı 한 단추로 바탕 화면, 예를 들어 문서를 모두 지정한 경로로 복사하려는 경우 문제가 있습니다. ı 목록 상자와 ı에서 모든 파일을 가져 오지 마십시오. t 사본은 당신이 버튼을 클릭 이벤트가있는 경우 클립 보드를들을 필요가 없기 때문에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 andy and 컴파일하지 않으면 체크 아웃 ...

+0

이 코드 오류 내가 당신에게 터키어를 말할 것이다 알고있는 경우 목록 상자 – leonf

+0

에서 모든 파일은 터키어 sevki 말을 할 수있다? – leonf

+0

개인 무효를 Button1_Click (개체 보낸 사람, EventArgs입니다 전자) – leonf

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

+0

물론 데스크탑에 이미 동일한 파일이 있으면 예외가 발생합니다. 기존 파일을 덮어 쓰려면 세 번째 File.Copy 메서드 매개 변수를 설정해야합니다. 편집 된 코드를 참조하십시오. – Reniuz

+0

@leonf 작동 방식을 익히고 디버깅하여 작동하므로 복사본 붙여 넣기 코드 응답을 기대하지 마십시오. – Amicable

관련 문제