Question: How in the name of the created document, insert the time spent on its creation?

string fileName = "prefix" + "_" + "время создания документа в формате ЧЧММССННН: ЧЧ" + "" + "_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".xml"; 
  • ":" - this sign is prohibited on the way, use "_" - kimaman2
  • Actually, I just stressed where the file creation time should be located. About the fact that you can not use ":" I know very well. - Macro
  • Read the question ten times to understand it. Speech is not about time format at all))). It's about how in the name of the created document, insert the time spent on its creation. - Macro
  • No need to be rude, it is better to write it clearly in the question, because I read it three times - there is only “time to create a document”, which is not clearly defined in Russian. - Monk

1 answer 1

 // Запускаем таймер Stopwatch makeFile = Stopwatch.StartNew(); // Создаем временный файл string tmpFile = Path.GetTempFileName(); // Что-то с ним делаем using (StreamWriter sw = new StreamWriter(tmpFile)) { for (int i = 0; i < 1000; i++) { sw.WriteLine(i.ToString()); } } // Останавливаем таймер makeFile.Stop(); // Берем полученный интервал времени (время затраченное на создание) TimeSpan ts = makeFile.Elapsed; // Дата создания файла DateTime dt = DateTime.Now; // Имя файла с временем, затраченным на создание string fileName1 = string.Format(@"C:\Users\admin\Documents\NewFile_{0:D2}-{1:D2}-{2:D2}_{3:D2}.txt", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds); // Имя файла с временем создания string fileName2 = string.Format(@"C:\Users\admin\Documents\NewFile_{0:D2}-{1:D2}-{2:D2}_{3:D2}.txt", dt.Hour, dt.Minute, dt.Second, dt.Millisecond); // Перемещаем и переименовываем временный файл File.Move(tmpFile, fileName1); // File.Move(tmpFile, fileName2);