Today you need to do a function, is the customer directly drag the file to the WinForm interface, and then display the file content.
In fact, this function point is to get the path of the dragged file.
It is generally necessary to set up three places:
1. The AllowDrop property of the control that accepts the file is set to True.
2. The control that accepts the file plus the DragDrop event.
This.dgv_openJsonFile1.Cursor = System.Windows.Forms.Cursors.Default; if (!e.data.getdatapresent (DataFormats.FileDrop)) { return; } var path = e.Data.GetData (DataFormats.FileDrop) as string[]; if (path! = null && path. Length > 0) { var jsonPath = path[0]; if (!string. IsNullOrEmpty (JsonPath)) { //Todo:jsonpath and file path ... } }
3. The control that accepts the file plus the DragEnter event.
if (E.data.getdatapresent (DataFormats.FileDrop)) { e.effect = Dragdropeffects.link; This.dgv_openJsonFile1.Cursor = System.Windows.Forms.Cursors.Arrow; } else { e.effect = DragDropEffects.None; }
Only three of the above can be
But I've been doing it for a long time.
Know the last to know, originally because of the permissions problem:
Drag-and-drop messages cannot be shared between programs that have different privilege elevation levels. You can do a simple experiment. Run Notepad with the administrator and drag the TXT file from Windows Explorer to find that the file does not open at all. Because Windows Explorer has a privilege elevation level of invokeasuser, not requireadministrator. The same level of privileges to elevate the account running program can be Share drag and drop. If the resources of the system are not involved, such as dragging files. Dragging within your own program is independent of the level of UAC.
My reason is because I ran the VS as an administrator. Remove run as administrator, and normal.
About. NET file drag and drop issues