將一個文件拖拽到窗體的某個控件時,將該控件的路徑顯示在該控件上,只要拿到了路徑自然可以讀取文件中的內容了
將一個控件的屬性AllowDrop設置為true,然后添加DragDrop、DragEnter時間處理函數,如下:
private void txtAppPath_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } private void txtAppPath_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { txtLocalFileName.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); }
