How do you move a file to another directory?
Ask by ICSM Computer
Updated 19 May 2025
In C#, you can move a file to another directory using the
File.Movemethod from theSystem.IOnamespace.Syntax:
sourceFileName: Full path of the file you want to move.destFileName: Full path where you want the file moved.Example:
This moves
file.txtfromC:\sourcetoC:\destination.Important Notes:
IOExceptionis thrown. There’s no built-inoverwriteoption forFile.Move, so you'd have to delete the destination file first if needed.DirectoryNotFoundExceptionis thrown.Optional: Overwrite manually if needed