C#中实现文件拖放打开的方法

 

设置Form属性 AllowDrop = True;

在Form事件中

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;
            }
        }

 

posted on 2020-02-28 15:10  永恒921  阅读(1147)  评论(1编辑  收藏  举报