我有一个表单,您可以拖放文件,我想知道如果数据是文件或文件夹,我怎么能知道应用程序。
我的第一次尝试是寻找“。”在数据,但然后一些文件夹确实有一个。在他们中。我也试过做一个File.Exists和一个Directory.Exists条件但是它只搜索当前的应用程序路径而不是其他任何地方。
无论如何,我可以以某种方式将.Exists应用于特定目录中,还是有办法检查拖入表单的数据类型?
答案 0 :(得分:16)
将路径指定为字符串,您可以使用System.IO.File.GetAttributes(string path)获取file's attributes。
FileAttributes attr = File.GetAttributes(path);
bool isFolder = (attr & FileAttributes.Directory) == FileAttributes.Directory;
答案 1 :(得分:1)
if(Directory.Exists(path))
// then it is a directory
else
// then it is a file