C# 拖曳檔案並取得檔案路徑

一定要用到DragEnter,要不然會沒有反應

所以一個物件至少要使用到DragEnter、DragDrop

DragEnter 拖移到視窗的事件

    private void DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;//调用DragDrop事件 
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        

DragDrop 拖移事件

底下的filePaths[0]就是檔案的路徑字串

        private void DragDrop(object sender, DragEventArgs e)
        {
            string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
            this.File_Path_TextBox.Text = filePaths[0];
        }
        
Last modification:October 8th, 2018 at 06:27 pm