locked
How can I rename a file in C#?

    Question

  • I realize this may sound like a pretty dumb thing to ask but, I just finished the Online Help and as a result am more confused than when I started! 

    I went to the C# Help, set the language filter to C# found the "RenameFile Method", noted that it was a new feature of the 2.0 Framework saw the syntax was in C#  . . .  and that was it.  Everything after that is in Visual Basic, even the Example, which wasn't very clear at all:

    Example

    This example renames the file Test.txt to SecondTest.txt.

    Change "C:\Test.txt" to the path and file name of the file that you want to rename.

     

    The Online help, in this case wasn't very helpful.

    I would really appreciate any assistance in this.  I clicked on the available link to the Visual Basic version and was not surprised to see that the example and page were the same as the C# version.

    Thank you;

    Greg

    Monday, November 06, 2006 2:40 PM

Answers

  • There IS a file rename method:

    System.IO.File.Move()

    You specify the old and new filenames. If the old and new files are on the same physical disk, the file is renamed. Otherwise, it is physically moved (copy made in new location, then - if that was successful - the old file deleted).


    Monday, November 06, 2006 3:09 PM
  • A better way is with the File.Move method:

    System.IO.File.Move(@"C:\From.txt", @"C:\TO.txt");

    Monday, November 06, 2006 3:11 PM
  • Hi Greg,

    You may want to try this as well, it's essentially the same as above with a slightly less code.

    File.Move(oldFileName, newFileName);

    Hope this helps,
    Jeremy
    Monday, November 06, 2006 3:11 PM
  • C# doesnot provide file rename function unfortunately. Any how the idea is to do this:

    File.Copy(oldFileName, NewFileName);

    File.Delete(oldFileName);

    That's It!

    I hope this will help!

    Best Regards,

    Monday, November 06, 2006 3:04 PM

All replies

  • C# doesnot provide file rename function unfortunately. Any how the idea is to do this:

    File.Copy(oldFileName, NewFileName);

    File.Delete(oldFileName);

    That's It!

    I hope this will help!

    Best Regards,

    Monday, November 06, 2006 3:04 PM
  • There IS a file rename method:

    System.IO.File.Move()

    You specify the old and new filenames. If the old and new files are on the same physical disk, the file is renamed. Otherwise, it is physically moved (copy made in new location, then - if that was successful - the old file deleted).


    Monday, November 06, 2006 3:09 PM
  • Hi Greg,

    You may want to try this as well, it's essentially the same as above with a slightly less code.

    File.Move(oldFileName, newFileName);

    Hope this helps,
    Jeremy
    Monday, November 06, 2006 3:11 PM
  • A better way is with the File.Move method:

    System.IO.File.Move(@"C:\From.txt", @"C:\TO.txt");

    Monday, November 06, 2006 3:11 PM
  • Thank you all for the replies!  I'll read the section on Moving files. 

     

    Greg

    Monday, November 06, 2006 3:26 PM
  • No question is a stupid question.  Now some pricks may think that and that just shows no character for those developers (nobody here of course).  If you can't find it online and you've really made a decent try at trying to figure out the syntax in VS using Intellisense, online blogs, books, or msdn, then sure, asking a "dumb" question is not "dumb" because every one of us has had to find this on our own at some point for more often the case or not.  A question is a question and some people think differently so they end up asking what is deemed "dumb" but really may not be even though it seems to be a "simple" question.

    C# Web Developer
    Tuesday, November 18, 2008 5:38 PM
  • A better way is with the File.Move method:

    System.IO.File.Move(@" C:\From.txt ", @" C:\TO.txt ");


    This will actually delete the file instead of renaming it.
    I tested it several times.
    Might be a bug, but that's the result you get.
    Saturday, July 04, 2009 1:00 PM
  • A better way is with the File.Move method:

    System.IO.File.Move(@" C:\From.txt ", @" C:\TO.txt ");


    This will actually delete the file instead of renaming it.
    I tested it several times.
    Might be a bug, but that's the result you get.
    Your test must be in error!

    Please post your test code, so we can point out what's wrong with it.

    (Incidentally, the File.Move() method just calls the Windows API "MoveFile" function.)
    Sunday, July 05, 2009 9:27 AM