Saturday 26 January 2013

Create a Directory in VB.NET

Create a Directory in VB.NET


Creating a directory in VB.NET can be done by two ways
          1. Static method part of Directory Class CreateDirectory
          2.Instance method of  DirectoryInfo Create.

Before that Call  imports System.IO;

Calling
                   CreateDirectory("C:\personal");

Method1   - Directory Static method

Shared  Sub CreateDirectory(ByVal dir As String)
    Try
        If Not Directory.Exists(dir) Then
            Dim dInfo As DirectoryInfo =  Directory.CreateDirectory(dir)
        End If
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Sub

Method 2 - DirectoryInfo Instance Method

 Shared  Sub CreateDirectory(ByVal dir As String)
    Try
        If Not Directory.Exists(dir) Then
            Dim dd As DirectoryInfo =  New DirectoryInfo(dir)
            dd.Create()
        End If
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
 End Sub

  •  Creating already existing directory nothing will happen(no content will be erased).
  • Directory name should contain volume label C:\ or D:\  otherwise Directory will be created in the current folder.  In order to get Current Directory  call Directory.GetCurrentDirectory()
tags:Create a directory in VB.NET, Creating a directory in VB.NET, Create a folder in VB.NET,How to create a directory in VB.NET,How to Create directory using DirectoryInfo, System.IO.Directory.CreateDirectory, System.IO.DirectoryInfo.Create, Directory.GetCurrentDirectory.

No comments:

Post a Comment