vb.net – How do I create a folder in VB if it doesnt exist?

vb.net – How do I create a folder in VB if it doesnt exist?

If Not System.IO.Directory.Exists(YourPath) Then
    System.IO.Directory.CreateDirectory(YourPath)
End If

Under System.IO, there is a class called Directory.
Do the following:

If Not Directory.Exists(path) Then
    Directory.CreateDirectory(path)
End If

It will ensure that the directory is there.

vb.net – How do I create a folder in VB if it doesnt exist?

Try the System.IO.DirectoryInfo class.

The sample from MSDN:

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
         Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo(c:MyDir)
        Try
             Determine whether the directory exists.
            If di.Exists Then
                 Indicate that it already exists.
                Console.WriteLine(That path exists already.)
                Return
            End If

             Try to create the directory.
            di.Create()
            Console.WriteLine(The directory was created successfully.)

             Delete the directory.
            di.Delete()
            Console.WriteLine(The directory was deleted successfully.)

        Catch e As Exception
            Console.WriteLine(The process failed: {0}, e.ToString())
        End Try
    End Sub
End Class

Leave a Reply

Your email address will not be published.