ファイルを移動する場合は、下記のように記述します。
|
|
'D:\test\test.txtをD:\移動先\test2.txtとして移動します。
'上書き指定ができませんので、Tryでやるか、存在確認を
'してからやった方がいいと思います。
Try
System.IO.File.Move("D:\test\test.txt","D:\移動先\test2.txt")
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
'存在確認をする場合
If System.IO.File.Exists("D:\移動先\test2.txt") = False Then
System.IO.File.Move("D:\test\test.txt","D:\移動先\test2.txt")
End If
|
|
|
上記の記述だと、移動先に同名のファイルがある場合には、移動処理を行わないことになります。
上書きしたい場合には、ファイルの存在を確認し、削除した上で移動しましょう。
|
'存在している場合は、削除してから移動
If System.IO.File.Exists("D:\移動先\test2.txt") = False Then
System.IO.File.Move("D:\test\test.txt","D:\移動先\test2.txt")
Else
System.IO.File.Delete("D:\移動先\test2.txt")
System.IO.File.Move("D:\test\test.txt","D:\移動先\test2.txt")
End If
'ちなみに、Deleteはファイルが無くても例外にはなりませんので、
'存在確認をせずに、削除→移動としても大丈夫です。
'(管理人的には、あまり好みませんが…)
System.IO.File.Delete("D:\移動先\test2.txt")
System.IO.File.Move("D:\test\test.txt","D:\移動先\test2.txt")
|
|