如何区分c#中的拖放事件中的文件或文件夹? [英] How do I distinguish a file or a folder in a drag and drop event in c#?

查看:133
本文介绍了如何区分c#中的拖放事件中的文件或文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单来拖放文件,我想知道如何让应用程序知道数据是文件还是文件夹。



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



有没有吗可以以某种方式将.Exists应用到特定的目录中,或者有什么方法可以检查什么类型的数据被拖入表单?

解决方案

将路径作为字符串,您可以使用 System.IO.File.GetAttributes(string path)获取文件的属性

  FileAttributes attr = File.GetAttributes(path); 
bool isFolder =(attr& FileAttributes.Directory)== FileAttributes.Directory;


I have a form that you drag and drop files into and I was wondering how can I make the application know if the data is a file or a folder.

My first attempt was to look for a "." in the data but then some folders do have a . in them. I've also tried doing a File.Exists and a Directory.Exists condition but then it only searches on the current application path and not anywhere else.

Is there anyway I can somehow apply the .Exists in a specific directory or is there a way I can check what type of data is dragged into the form?

解决方案

Given the path as a string, you can use System.IO.File.GetAttributes(string path) to get file's attributes.

FileAttributes attr = File.GetAttributes(path);
bool isFolder = (attr & FileAttributes.Directory) == FileAttributes.Directory;

这篇关于如何区分c#中的拖放事件中的文件或文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆