C# 窗体打开拖动到窗体的文件

时间:2022-04-03 15:50:56
 1         private void Form3_DragEnter(object sender, DragEventArgs e)
 2         {
 3             if (e.Data.GetDataPresent(DataFormats.FileDrop))
 4             {
 5                 e.Effect = DragDropEffects.All;    
 6             }
 7             else
 8             {
 9                 e.Effect = DragDropEffects.None;
10             }
11 
12         }
13 
14         private void Form3_DragDrop(object sender, DragEventArgs e)
15         {
16             string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
17             if (null != filePaths && filePaths.Length > 0)
18             {
19                 foreach (string item in filePaths)
20                 {
21                     var lines = System.IO.File.ReadAllLines(item);
22                     Array.ForEach(lines.ToArray(), n => richTextBox1.AppendText(n.ToString() + "\r\n"));
23                 }
24             }
25         }