Rename a file in C#

后端 未结 18 855
北荒
北荒 2020-11-22 15:05

How do I rename a file using C#?

18条回答
  •  忘了有多久
    2020-11-22 15:30

    In my case, I want the name of the renamed file to be unique, so I add a date-time stamp to the name. This way, the filename of the 'old' log is always unique:

    if (File.Exists(clogfile))
    {
        Int64 fileSizeInBytes = new FileInfo(clogfile).Length;
        if (fileSizeInBytes > 5000000)
        {
            string path = Path.GetFullPath(clogfile);
            string filename = Path.GetFileNameWithoutExtension(clogfile);
            System.IO.File.Move(clogfile, Path.Combine(path, string.Format("{0}{1}.log", filename, DateTime.Now.ToString("yyyyMMdd_HHmmss"))));
        }
    }
    

提交回复
热议问题