我怎样才能得到拖拽的路径删除文件 [英] How can I get the path of the drag & dropped files

查看:87
本文介绍了我怎样才能得到拖拽的路径删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我想在我的列表框中拖放一个文件,按下按钮后,该文件应该被复制到另一个路径。

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





按钮:

  private   void  CleanButton_Click( object  sender,RoutedEventArgs e)
{
string CC = Convert.ToString( C://用户//詹姆斯//桌面// VB //图片);
string hallo = Convert.ToString(e.Data.GetDataPresent(DataFormats.FileDrop));


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





该剂量有效。我怎样才能获得路径?

解决方案

您好,



我会使用变量来存储路径,然后在按钮事件中使用它。



这是因为按钮事件arg与drop方法事件args不同,你不能重复使用它。 br $>


 私人  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!);
}


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.

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:

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?

解决方案

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.

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!");
}


这篇关于我怎样才能得到拖拽的路径删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆