如何在c#中拖放事件中区分文件或文件夹?

我有一个表单,您可以拖放文件,我想知道如果数据是文件或文件夹,我怎么能知道应用程序。

我的第一次尝试是寻找一个“。” 在数据但然后一些文件夹确实有一个。 在他们中。 我也试过做一个File.Exists和一个Directory.Exists条件但是它只搜索当前的应用程序路径而不是其他任何地方。

无论如何我可以以某种方式将.Exists应用于特定目录中,或者有没有办法可以检查拖入表单的数据类型?

将路径指定为字符串,您可以使用System.IO.File.GetAttributes(字符串路径)来获取文件的属性 。

 FileAttributes attr = File.GetAttributes(path); bool isFolder = (attr & FileAttributes.Directory) == FileAttributes.Directory; 
 if(Directory.Exists(path)) // then it is a directory else // then it is a file