2017-07-18 35 views
0

我目前正在使用文件對話框導出文件,但我想知道如何使用拖放功能導出文件。我無法弄清楚如何獲取項目被刪除的文件路徑。這是我用來打開文件對話框的代碼,以防需要時使用。如何使用拖放來導出文件(C#)

if (this.listView1.SelectedItems.Count > 0) 
{ 
    ListViewItem item = this.listView1.SelectedItems[0]; 
    string text = this.faderLabel8.Text; 
    if (!text.EndsWith(@"\")) 
    { 
     text = text + @"\"; 
    } 

    using (SaveFileDialog dialog = new SaveFileDialog()) 
    { 
     if (dialog.ShowDialog() == DialogResult.OK) 
     { 
      Jtag.ReceiveFile(item.SubItems[0].Text, text + item.SubItems[0].Text); 
     } 
    } 
} 

回答

0

您不需要放置文件的路徑。相反,您需要創建一個臨時文件。

  1. 保存文件到一個臨時文件夾
  2. 啓動拖動事件/命令,諸如鼠標向下,在下面的方式:
//(This example is uses WPF/System.Windows.DragDrop) 
//Create temporary file 
string fileName = "DragDropSample.txt"; 
var tempPath = System.IO.Path.GetTempPath(); 
var tempFilePath = System.IO.Path.Combine(tempPath, fileName); 
System.IO.File.WriteAllText(tempFilePath, "Testing drag and drop"); 
//Create DataObject to drag 
DataObject dragData = new DataObject(); 
dragData.SetData(DataFormats.FileDrop, new string[] { tempFilePath }); 
//Initiate drag/drop 
DragDrop.DoDragDrop(dragSourceElement, dragData, DragDropEffects.Move); 

對於WinForms的例子和更多細節請參見: Implement file dragging to the desktop from a .net winforms application?

+0

感謝這工作 – RunFTL

0

如果你想讓它通過是有用的「拖放」你會需要某種顯示的容器中他們的文件,然後另一個容器,要移動它們的圖形界面。當你用鼠標突出顯示一個項目時,將它們添加到itemList中,當你放下它們時,將它們複製。只要確保清單清空一次,以防止刪除突出顯示。