2013-01-12 33 views
1

我知道下面的問題有點棘手,但我真的需要解決它...如何從RichTextBox控件中的拖放文本中將文件寫入到Windows資源管理器?

想象一下你有一個帶有文本框控件(在我的情況下是RichTextBox)的窗體。 現在,您選擇此控件中的文本的一部分,啓動拖動事件,並將選定的文本刪除窗體,右窗口資源管理器內的某個文件夾,例如桌面...

這怎麼可能完成?除了以下鏈接,我沒有在網上找到有用的東西:http://forums.asp.net/t/1600192.aspx/1

我所知道的是,這種文件寫入操作被稱爲「廢料文件生成」。 有人給我一個有用的提示嗎?

+0

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

好的,謝謝約翰;-) – oren

回答

2

FileDrop預計文件存在,並且drop只是做一個文件的副本。您必須將數據寫入文件,然後將該名稱傳遞給DataObject。下面只是一個演示,您需要弄清楚如何編寫文件並清理所有文件,以免在用戶的PC上創建額外的文件。

private void richTextBox1_MouseLeave(object sender, EventArgs e) 
{ 
    // If the left mouse button is down when leaving the rtb 
    if (MouseButtons == MouseButtons.Left) 
    { 
     // Write everything to a temp file. 
     System.IO.File.WriteAllText(@"z:\Temp\helloWorld.rtf", richTextBox1.SelectedRtf); 
     string[] filenames = { @"z:\Temp\helloWorld.rtf" }; 
     DataObject obj = new DataObject(); 
     // Set the drag drop data with the FileDrop format 
     obj.SetData(DataFormats.FileDrop, filenames); 
     // Start the drag drop effect 
     DoDragDrop(obj, DragDropEffects.All); 
    } 
} 
+0

哇!我真的不相信這會很容易!非常感謝John ;-) – oren

相關問題