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()
No comments:
Post a Comment