Saturday 26 January 2013

Rename a Directory or folder in VB.NET

Rename a Directory or folder in Vb.NET


Rename a folder in VB.NET/Move a folder in Vb.NET Can be done by 2 ways.
1.Static Method of Directory class Move
2.Instance method of DirectoryInfo MoveTo
Note: Moving a folder content to different Volume  throws an exception.
for this  user must copy all files to different volume/network drive/USB etc., then delete Source Content. Pls see other other articles .
Before that Call  imports System.IO;
 

Calling

Move_or_Rename_Directory("c:\personal", "c:\mypersonal");

 

Method1   - Directory Static method 

Shared  Sub Move_or_Rename_Directory(ByVal srcDir As String, ByVal destDir As String)
        Try
            Directory.Move(srcDir, destDir)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
 End Sub
    }

Method 2 - DirectoryInfo Instance Method 


  Shared  Sub Move_or_Rename_Directory(ByVal srcDir As String, ByVal destDir As String)
        Try
            Dim dInfo As DirectoryInfo =  New DirectoryInfo(srcDir)
            dInfo.MoveTo(destDir)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
  End Sub
   
   //output
check 
C:\mypersonal
user may get following exceptions
  • Could not find a part of the path 'c:\personal'. (if source folder "personal" does not exists).
  • Calling Move on different Volumes may result. i.e C:\ to D:\(vice versa) "Source and destination path must have identical roots. Move will not work across
     volumes
    "
Tags: Move a directory in VB.NET,Move a folder in VB.NET,Move a directory Structure to Different Location,Move a folder Structure to Different Location VB.NET, Move Directory with Content to Different Folder, Move all content to Different Folder,Move content to another name,Move all content to Different Directory, Rename a Directory VB.NET,Rename a Folder in VB.NET,How to rename a folder in VB.NET,Directory.Move,DirectoryInfo.MoveTo,System.IO,Move a folder in the same volume VB.NET
 

No comments:

Post a Comment