.NET:比位掩码更简单的目录测试方法?
有没有比使用位掩码更简单的方法来测试文件系统项是否是目录?
我的一个应用程序中有此代码(第二行实际上是在folderItems 上的循环中,但为了简单起见,假设第一个元素):(
Dim folderItems As String() = Directory.GetFileSystemEntries(aFolder)
Dim someDirItem As String = folderItems(0)
Dim fInfo As System.IO.FileInfo = New System.IO.FileInfo(someDirItem)
Dim isDirectory As Boolean = (CInt(fInfo.Attributes) And CInt(FileAttributes.Directory)) > 0
FileAttributes.Directory 为16)。
这可行,但是有没有比对 1000(基数 2)使用按位 AND 更简单的方法呢?
Is there an easier way to test if a file system item is a directory than using bitmasks?
I have this code in one of my applications (two second line is actually in a loop over folderItems but for simplicity assume the first element):
Dim folderItems As String() = Directory.GetFileSystemEntries(aFolder)
Dim someDirItem As String = folderItems(0)
Dim fInfo As System.IO.FileInfo = New System.IO.FileInfo(someDirItem)
Dim isDirectory As Boolean = (CInt(fInfo.Attributes) And CInt(FileAttributes.Directory)) > 0
(FileAttributes.Directory is 16).
This works, but is there an easier way than using bitwise AND with 1000 (base 2)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
怎么样:
如果 fullPath 是目录,则返回 true。
How about:
Returns true if fullPath is a directory.
您可以使用
Directory.Exists(path)
不是吗?担
You could use
Directory.Exists(path)
couldn't you?Dan
您可以为测试按位标志的枚举创建扩展方法。像这样:
然后你就可以调用:
抱歉,那是 C#,但转换起来应该不难,我只是不知道泛型的 VB 语法。任何人都可以随意编辑并添加 VB 翻译。
You could create an extension method for enumerations that tests bitwise flags. Something like this:
Then you would just call:
Sorry, that's C#, but it shouldn't be hard to convert, I just don't know my VB syntax for generics. Anyone who does, feel free to edit and add the VB translation.
执行比较时不需要使用
CInt
:You don't need to use
CInt
when performing the comparison:按位比较很难吗?如果您真的不喜欢这样做,只需编写一个公开布尔数据结构的实用程序类,然后将其移植到您的项目中即可。
Bitwise comparisons are difficult? If you really don't like doing it this way just write a utility class that exposes a data structure of booleans and just port it around in your projects.