You can use the RenameFile method of the My.Computer.FileSystem object to rename a file by providing source file name with full path and the new file name.
You can use this method as:
file : File to be renamed. this is required parameter.
newName : New name of file. this is also required parameter.
This example renames the file hello.txt to helloWorld.txt which is located in
There are many possible exception of the above code such as if code doesn’t find the file with the specified name or code does not have PathDiscovery permission, code will throw
FileNotFoundException exception, similarly if current user has not necessary permissions to view the path, code will throw
SecurityExceptionand if user does not have the required permission
UnauthorizedAccessExceptionwill be thrown.
The file path should not be excess than system defined max length so generally program doesn’t support the file path excess than 260 characters, specially if your program is running prior to the .NET Framework 4.6.2, the file path should not be greater than 260 characters. In this situation code will throw
Similarly you can change the folder name using
You can use the below method:
And you can write your code as:
Mostly all above exceptions will also applied here.