> ## Content Index
> Fetch the complete content index at: https://blog.tsd.digital/llms.txt
> Use this file to discover other available public pages before exploring further.

# C# FileInfo.MoveTo Cannot create a file when that file already exists exception
- URL: https://blog.tsd.digital/c-fileinfomoveto-cannot-create-a-file-when-that-file-already-exists-exception/
- Published: 2009-05-12T18:39:35.000Z
- Updated: 2009-05-12T18:39:35.000Z
- Author: Tim Gaunt
- Tags: ASP.Net, C#, Development, .Net, #Import 2025-04-01 05:37

This was one of those irritating errors that you get when you're trying to do something quickly before you go home and you can't for the life of you fathom the issue. 

I had the following code (simple enough):

FileInfo f = new FileInfo("##File'sPath"); try{...}{ f.MoveTo("##DROPOFFDIRECTORY##")); }catch (Exception e) {...}{ //Logtheexceptionhere}

The fix was simple, you just have to remember to specify the new filename too. (DOH!). Here's the "correct" code.

FileInfo f = new FileInfo("## File's Path"); try{...}{ f.MoveTo(Path.Combine("## DROP OFF DIRECTORY ##", f.Name)); }catch (Exception e) {...}{ //Log the exception here}

Hope that helps you out ;)