1. private void listBox1_DragEnter(object sender, DragEventArgs e)
  2. {
  3. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  4. {
  5. e.Effect = DragDropEffects.Copy;
  6. }
  7. else
  8. {
  9. e.Effect = DragDropEffects.None;
  10. }
  11. }
  12.  
  13.  
  14. private void listBox1_DragDrop(object sender, DragEventArgs e)
  15. {
  16. string[] test = (string[])e.Data.GetData(DataFormats.FileDrop, false);
  17. //MessageBox.Show(test[0].ToString());
  18. Tps1.Clear();
  19. listBox1.Items.Clear();
  20. try
  21. {
  22. if (test[0].ToString().Contains(".txt"))
  23. {
  24. StreamReader sr1 = new StreamReader(test[0].ToString(), Encoding.UTF8);
  25. string line1;
  26. while ((line1 = sr1.ReadLine()) != null)
  27. {
  28. if (line1.Trim() == "")
  29. continue;
  30. Tps1.Add(line1);
  31. listBox1.Items.Add(line1);
  32. }
  33. sr1.Close();
  34. sr1.Dispose();
  35. }
  36. }
  37. catch { MessageBox.Show("文件不能识别!"); }
  38. }
04-18 09:40