1. 程式人生 > 其它 >C#中實現檔案拖放開啟的方法

C#中實現檔案拖放開啟的方法

技術標籤:C#教程c#教程

設定Form屬性 AllowDrop = True;

在Form事件c#教程

private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string localFilePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//檔案完整路徑
            string extension = System.IO.Path.GetExtension
(localFilePath);//檔案字尾名 if (extension == ".cbd") {         //todo you code } } private void Form1_DragEnter(object sender, DragEventArgs e) { string localFilePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)
).GetValue(0).ToString(); string extension = System.IO.Path.GetExtension(localFilePath); if (string.Compare(extension, ".cds", true) == 0) { e.Effect = DragDropEffects.Move; } else { e.Effect =
DragDropEffects.None; } }