Create a Sub Directories using VB.NET
Creating a Sub directory in VB.NET can be done by two ways
1. Static method part of Directory Class CreateDirectory
2.Instance method of DirectoryInfo CreateSubdirectory.
Before that Call imports System.IO;
Calling
CreateSubDirectory("c:\personal", "Excel Files");
Method1 - Directory Static method
Shared Sub CreateSubDirectory(ByVal src As String, ByVal subDir As String)Try
Directory.CreateDirectory(Path.Combine(src, subDir))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Method 2 - DirectoryInfo Instance Method
Shared Sub CreateSubDirectory(ByVal src As String, ByVal subDir As String)Try
Dim dInfo As DirectoryInfo = New DirectoryInfo(src)
'Here relative path should be given i.e just folder name i.e "Excel Files"
dInfo.CreateSubdirectory(subDir)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
//output
C:\personal\Excel files\
- Creating already existing sub directory nothing will happen(no content will be erased).
- If main directory doesn't exists , in that case src directory will be created then sub directory.
- 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