none
How can I rename a file in C#?

    Question

  • There are 4 threads on this subject which all say use File.Move or File.copy and File.delete.

    However both suggestions result in a file with today's date and time. Rename leaves those attributes alone. My program really needs the time stamp left alone.

    Monday, March 12, 2012 5:42 AM

Answers

  • I think you can leverage CMD like this

    Process.Start("cmd", @"ren c:\a.txt b.txt");


    Regards, Nighting Liu

    Monday, March 12, 2012 5:49 AM
  • If you want to retain the creation, modification and access time, you can use the following code:

    string source = "D:\\rhd.txt";
    string dest = "C:\\rhd.txt";
    DateTime ct = File.GetCreationTime(source);
    DateTime at = File.GetLastAccessTime(source);
    File.Move(source, dest);
    File.SetCreationTime(dest, ct);
    File.SetLastAccessTime(dest, at);

    • Proposed as answer by Heslacher Monday, March 12, 2012 6:57 AM
    • Marked as answer by Mike D G Monday, March 12, 2012 8:48 AM
    Monday, March 12, 2012 6:37 AM
  • Hello Mike You can use 

     Microsoft.VisualBasic.FileSystem.Rename("Old Path", "New Path"); 

    For this add reference to Microsoft.VisualBasic to your project.

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.rename.aspx


    Want to add MVP with my name.

    Monday, March 12, 2012 6:53 AM

All replies

  • I think you can leverage CMD like this

    Process.Start("cmd", @"ren c:\a.txt b.txt");


    Regards, Nighting Liu

    Monday, March 12, 2012 5:49 AM
  • If you need to retain the old dates.

    1. Rename the file using File.Move()

    2. Retail old dates using File.SetCreationTime, File.SetLastAccessTime


    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Proposed as answer by Heslacher Monday, March 12, 2012 6:57 AM
    Monday, March 12, 2012 5:53 AM
  • If you want to retain the creation, modification and access time, you can use the following code:

    string source = "D:\\rhd.txt";
    string dest = "C:\\rhd.txt";
    DateTime ct = File.GetCreationTime(source);
    DateTime at = File.GetLastAccessTime(source);
    File.Move(source, dest);
    File.SetCreationTime(dest, ct);
    File.SetLastAccessTime(dest, at);

    • Proposed as answer by Heslacher Monday, March 12, 2012 6:57 AM
    • Marked as answer by Mike D G Monday, March 12, 2012 8:48 AM
    Monday, March 12, 2012 6:37 AM
  • Hi Mike D G,

      This belongs to file manipulations included in System.IO namespace,so you'd better use using command to add it.

      You can read the MSDN article such as http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx ,or you can refer to other blog article as follows:

    http://www.dotnetperls.com/file-move 

      I hope it will solve your problem.

    Sincerely,

    Jason Wang


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, March 12, 2012 6:43 AM
  • Hello Mike You can use 

     Microsoft.VisualBasic.FileSystem.Rename("Old Path", "New Path"); 

    For this add reference to Microsoft.VisualBasic to your project.

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.rename.aspx


    Want to add MVP with my name.

    Monday, March 12, 2012 6:53 AM
  • Thanks, Rashid and everyone else.

    Rashid's suggestion works a treat.

    Monday, March 12, 2012 9:27 AM
  • Its great that you got the solution. You can also check the proposed answers.As these also contain your solution in different way.

     Good luck


    Want to add MVP with my name.

    Monday, March 12, 2012 9:31 AM