.NET:比位掩码更简单的目录测试方法?

发布于 2024-08-05 11:31:38 字数 496 浏览 1 评论 0原文

有没有比使用位掩码更简单的方法来测试文件系统项是否是目录?

我的一个应用程序中有此代码(第二行实际上是在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不乱于心 2024-08-12 11:31:38

怎么样:

System.IO.Directory.Exists(fullPath)

如果 fullPath 是目录,则返回 true。

How about:

System.IO.Directory.Exists(fullPath)

Returns true if fullPath is a directory.

停顿的约定 2024-08-12 11:31:38

您可以使用 Directory.Exists(path) 不是吗?

You could use Directory.Exists(path) couldn't you?

Dan

甜柠檬 2024-08-12 11:31:38

您可以为测试按位标志的枚举创建扩展方法。像这样:

public static bool Has<T>(this System.Enum type, T value)
{
    try
    {
        return (((int)(object)type & (int)(object)value) == (int)(object)value);
    }
    catch
    {
        return false;
    }
}

然后你就可以调用:

Bool isDirectory = fInfo.Attributes.Has(FileAttributes.Directory))

抱歉,那是 C#,但转换起来应该不难,我只是不知道泛型的 VB 语法。任何人都可以随意编辑并添加 VB 翻译。

You could create an extension method for enumerations that tests bitwise flags. Something like this:

public static bool Has<T>(this System.Enum type, T value)
{
    try
    {
        return (((int)(object)type & (int)(object)value) == (int)(object)value);
    }
    catch
    {
        return false;
    }
}

Then you would just call:

Bool isDirectory = fInfo.Attributes.Has(FileAttributes.Directory))

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.

梦屿孤独相伴 2024-08-12 11:31:38
Dim folderItems As String() = Directory.GetFileSystemEntries(aFolder)
Dim someDirItem As String = folderItems(0)
Dim isDirectory As Boolean = System.IO.Directory.Exist(someDirItem)
Dim folderItems As String() = Directory.GetFileSystemEntries(aFolder)
Dim someDirItem As String = folderItems(0)
Dim isDirectory As Boolean = System.IO.Directory.Exist(someDirItem)
[旋木] 2024-08-12 11:31:38

执行比较时不需要使用 CInt

Dim isDirectory As Boolean = _
    (fInfo.Attributes And FileAttributes.Directory) = FileAttributes.Directory

You don't need to use CInt when performing the comparison:

Dim isDirectory As Boolean = _
    (fInfo.Attributes And FileAttributes.Directory) = FileAttributes.Directory
江湖彼岸 2024-08-12 11:31:38

按位比较很难吗?如果您真的不喜欢这样做,只需编写一个公开布尔数据结构的实用程序类,然后将其移植到您的项目中即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文