VB.Net Tutorial/Stream File/DirectoryInfo

Материал из VB Эксперт
Перейти к: навигация, поиск

Create DirectoryInfo and get its parent and root

Imports System.IO
Public Class Tester
    Public Shared Sub Main

        Dim myDirectoryInfo As DirectoryInfo
        myDirectoryInfo = New DirectoryInfo("c:\\Java_dev")
        Console.WriteLine(myDirectoryInfo.Exists)
        
        Console.WriteLine(myDirectoryInfo.Name)
        Console.WriteLine(myDirectoryInfo.Parent.ToString)
        Console.WriteLine(myDirectoryInfo.Root.ToString)
    End Sub
End Class
True
Java_dev
c:\

DirectoryInfo: create directory and sub directory

Imports System.IO
Public Class Tester
    Public Shared Sub Main
    
        Dim DirInfo As System.IO.DirectoryInfo
        DirInfo = System.IO.Directory.CreateDirectory("c:\a")
        DirInfo.CreateSubdirectory("a")
    
    End Sub
End Class

Get all directories under current directory

Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim myDirectoryInfo As DirectoryInfo
        myDirectoryInfo = New DirectoryInfo("c:\\Java_dev")

        Dim myDirectoryInfos() As DirectoryInfo
        Dim myDir As DirectoryInfo
        Try
            myDirectoryInfos = myDirectoryInfo.GetDirectories()
            For Each myDir In myDirectoryInfos
                Console.WriteLine(myDir.FullName)
            Next
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class
c:\Java_dev\backup
c:\Java_dev\sdk
c:\Java_dev\support
c:\Java_dev\WEB

Get all files under a directory and output file name

Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim myDirectoryInfo As DirectoryInfo
        myDirectoryInfo = New DirectoryInfo("c:\\")

        Dim filesInfo() As FileInfo
        Dim fileInfo As FileInfo
        Try
            filesInfo = myDirectoryInfo.GetFiles()
            For Each fileInfo In filesInfo
                Console.WriteLine(fileInfo.FullName)
            Next
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class
c:\AUTOEXEC.BAT
c:\boot.ini
c:\CONFIG.SYS
c:\DebugMessages.txt
c:\hiberfil.sys
c:\IO.SYS
c:\MSDOS.SYS
c:\NTDETECT.ru
c:\ntldr
c:\pagefile.sys
c:\test.txt
c:\test1.txt

Get directories under current directory with condition

Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim myDirectoryInfo As DirectoryInfo
        myDirectoryInfo = New DirectoryInfo("c:\\")

        Dim myDirectoryInfos() As DirectoryInfo
        Dim myDir As DirectoryInfo
        Try
            myDirectoryInfos = myDirectoryInfo.GetDirectories("wind*")
            For Each myDir In myDirectoryInfos
                Console.WriteLine(myDir.FullName)
            Next
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class
c:\WINDOWS

Get files under a directory with condition

Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Dim myDirectoryInfo As DirectoryInfo
        myDirectoryInfo = New DirectoryInfo("c:\\")

        Dim filesInfo() As FileInfo
        Dim fileInfo As FileInfo
        Try
            filesInfo = myDirectoryInfo.GetFiles("*.sys")
            For Each fileInfo In filesInfo
                Console.WriteLine(fileInfo.FullName)
            Next
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class
c:\CONFIG.SYS
c:\hiberfil.sys
c:\IO.SYS
c:\MSDOS.SYS
c:\pagefile.sys