-1

我将我的文件发送到剪贴板,我想复制所有文件指定路径,例如一键单击桌面,文件,例如但我有问题我不从列表框中获取所有文件,我不复制指定路径我如何复制所有文件……

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

2 回答 2

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,而且我还没有检查它是否编译...

于 2012-04-24T10:00:13.057 回答
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);
 }
于 2012-04-24T10:01:06.850 回答