Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I want to drag and drop a file in my Listbox and after pressing a button, this file should be copied to another Path.
C#
public void DropBox_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                DropBox.Items.Clear();
                
                string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

                foreach (string droppedFilePath in droppedFilePaths)
                {
                    ListBoxItem fileItem = new ListBoxItem();

                    fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath);
                    fileItem.ToolTip = droppedFilePath;

                    DropBox.Items.Add(fileItem);
                }
            }
        }


Button:
C#
private void CleanButton_Click(object sender, RoutedEventArgs e)
       { 
            string CC= Convert.ToString("C://Users//James//Desktop//VB//Pictures");       
            string hallo =Convert.ToString(e.Data.GetDataPresent(DataFormats.FileDrop));
       

          System.IO.File.Copy(hallo, CC);
         //   MessageBox.Show("Kopieren erfolgreich!");
       }


That dosent work. How can I get the path?
Posted

1 solution

Hello,

I would use a variable to store the path, then use it in the button event.

This is because the button event arg is not the same as the drop method event args, you cannot reuse it.

C#
private string _droppedFilePath;

 public void DropBox_Drop(object sender, DragEventArgs e)
 {
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
         DropBox.Items.Clear();

         string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

         foreach (string droppedFilePath in droppedFilePaths)
         {
             ListBoxItem fileItem = new ListBoxItem();

             fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath);
             fileItem.ToolTip = droppedFilePath;

             DropBox.Items.Add(fileItem);

             _droppedFilePath = droppedFilePath;
         }
     }
 }

private void CleanButton_Click(object sender, RoutedEventArgs e)
{
     string CC= Convert.ToString("C://Users//James//Desktop//VB//Pictures");

     System.IO.File.Copy(_droppedFilePath, CC);
  //   MessageBox.Show("Kopieren erfolgreich!");
}
 
Share this answer
 
Comments
Member 11685336 12-May-15 15:28pm    
Thank you!
Now I geht this if I drag and drop a file and press on the button:
System.IO.DirectoryNotFoundException
He cant find the Path

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900