c#将文件复制到指定路径

时间:2012-04-24 09:32:01

标签: c# listbox clipboard

ı发送我的文件到cliboard和ı想要复制所有文件指定的路径,例如一个按钮单击桌面,文件,但我的问题ı没有从列表框中获取所有文件,我不复制指定的路径如何ı可以复制所有文件......

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 
    }
}

2 个答案:

答案 0 :(得分: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方便,我还没有检查它是否编译...

答案 1 :(得分: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);
 }