如何在不知道特定目录名称的情况下检查是否存在任何目录,而只知道该文件夹可能位于的文件夹

发布于 2024-08-29 13:10:04 字数 85 浏览 2 评论 0原文

在 vb.net 中,如何检查目录中是否存在任何目录

我需要知道 c:\windows 目录中是否存在文件夹(不知道其中是否存在任何目录)。

In vb.net how do you check if a ANY directory exists inside a directory

I would need to know if there is a folder inside the c:\windows directory (WITHOUT knowing if there is ANY directory is in there).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

森末i 2024-09-05 13:10:04

那么你想检查一个目录中是否有子目录?很公平:

Dim hasSubDirectories as Boolean = My.Computer.FileSystem.GetDirectories(parentDir).Count > 0

So you want to check to see if there are subdirectories in a directory? Fair enough:

Dim hasSubDirectories as Boolean = My.Computer.FileSystem.GetDirectories(parentDir).Count > 0
眉黛浅 2024-09-05 13:10:04

您可以在 System.IO 命名空间内使用 DirectoryInfo 类。

例子:

    Dim path As String = "C:\Windows"
    Dim directoryInfo As New DirectoryInfo(path)

    Dim dirInfos() As DirectoryInfo = directoryInfo.GetDirectories()

    If (dirInfos.Length > 0) Then
        ' you have directories, do what you want
    End If

    'or iterate over directories

    For Each dirInfo As DirectoryInfo In dirInfos
        ' do something with each directory
    Next

You can use the DirectoryInfo class inside the System.IO namespace.

Example:

    Dim path As String = "C:\Windows"
    Dim directoryInfo As New DirectoryInfo(path)

    Dim dirInfos() As DirectoryInfo = directoryInfo.GetDirectories()

    If (dirInfos.Length > 0) Then
        ' you have directories, do what you want
    End If

    'or iterate over directories

    For Each dirInfo As DirectoryInfo In dirInfos
        ' do something with each directory
    Next
我一直都在从未离去 2024-09-05 13:10:04

与使用 mattbasta 建议的 VB 特定函数不同,使用 System.IO.Directory 类也同样简单,该类是 BCL 的一部分,任何其他 .NET 开发人员都会熟悉该类。

Dim hasSubDirectories = System.IO.Directory.GetDirectories(parentPath).Length > 0

Rather than use a VB-specific function like mattbasta suggests, it's just as easy to use the System.IO.Directory class, which is part of the BCL and would be familiar to any other .NET developer.

Dim hasSubDirectories = System.IO.Directory.GetDirectories(parentPath).Length > 0
深海蓝天 2024-09-05 13:10:04

问题是我无法转换为字符串

    Dim path As String = "..\..\..\Tier1 downloads\CourseVB\"


    If countNumberOfFolders > 0 Then 'if there is a folder then


        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(path)
        Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
        Dim dra As IO.DirectoryInfo

        'list the names of all files in the specified directory
        For Each dra In diar1

            Dim lessonDirectoryName() As Lesson
            lessonDirectoryName(0).lessonName = dra

        Next

'课程是一个对象,而lessonName是字符串类型的属性。如何将目录信息转换为字符串?

Problem is that i cant convert to string

    Dim path As String = "..\..\..\Tier1 downloads\CourseVB\"


    If countNumberOfFolders > 0 Then 'if there is a folder then


        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(path)
        Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
        Dim dra As IO.DirectoryInfo

        'list the names of all files in the specified directory
        For Each dra In diar1

            Dim lessonDirectoryName() As Lesson
            lessonDirectoryName(0).lessonName = dra

        Next

'the the lesson is an object, and lessonName is the property of type string. How do i convert the directoryInfo to string?

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