0

I am wanting to use File.Copy to copy a file to a directory. That is straightforward. Does File.Copy have an overload that can rename if the file already exists, or do I need to bundle File.Copy with File.Exist

  • 2
    Have you consulted MSDN? – JustAPup Apr 09 '15 at 13:44
  • http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp – Xi Sigma Apr 09 '15 at 13:44
  • @Minh - I saw it had an overwrite parameter I didn't see it had a rename one. – MasterOfStupidQuestions Apr 09 '15 at 13:46
  • 3
    how could it rename it? two files are defined to be the "same" if they have the same name, how would it know what file to rename to what? – DLeh Apr 09 '15 at 13:46
  • @DLeh - I am needing to copy a few files from test over to production (which may or may not exist). If the file already exist, I was going to rename by appending the date to the end of the filename. Does that help clarify my initial question? – MasterOfStupidQuestions Apr 09 '15 at 13:51
  • You could have easily discovered this on your own with a little research on the [docs](https://msdn.microsoft.com/en-us/library/system.io.file%28v=vs.110%29.aspx) – chancea Apr 09 '15 at 13:51
  • It's not such a strange question, Windows Explorer does just this. But I think Explorer has its own logic for that, it's not an OS function. – H H Apr 09 '15 at 14:12

3 Answers3

1

There is no such overload, so you have to use File.Exists and File.Copy.

andreycha
  • 383
  • 4
  • 22
1

As far as i am aware File.copy does not have an overload that allows renaming. https://msdn.microsoft.com/de-de/library/system.io.file.copy%28v=vs.110%29.aspx There is one to allow over writing and one to make a plain copy.

joachim
  • 652
  • 2
  • 7
  • 23
1

I'd go with File.Move to rename a file:

https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx

and yes - you'll still want to check its existence with File.Exists

mp3ferret
  • 1,183
  • 11
  • 16