Moving/ Coping a file.

samsmithnz

Well-known member
Joined
Jul 22, 2003
Messages
1,038
Location
Boston
Im trying to do a fairly simple move of a file from one folder to another. But Im having a little bit of trouble understanding the best way of handling it.

Currently I have:
Code:
Dim objFile As System.IO.File

Move the file to a new location
objFile.Move(strSourceFilePath, strTargetPath)

The thing is I have a filename variable (holding "C:\projects\files\file.txt"), and Id like to move it to another folder ("C:\projects"), but if the file already exists, I need to delete/overwrite it. The problem is I need to take the filename from the strSourceFilePath, and append it to the strTargetPath variable.

Im sure there used to be an easy way to drop the path and extract the file name in the old FileSystemObject... How do I do that in .NET (without using the split function, there must be a cleaner way)

thanks

Sam
 
There most certainly is a cleaner way:

Code:
Dim filename As String = System.IO.Path.GetFileName("c:\whatever.txt")
 
Back
Top