I want to create a variable in my c# app, of type DateTime, that stores the current date and time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to create a variable in my c# app, of type DateTime, that stores the current date and time?

I have a FileWriteAllText method, and I want to add the date and time to my file of type "bat"!

5th Dec 2016, 10:16 AM
Mitran Constantin Bogdan
Mitran Constantin Bogdan - avatar
2 Answers
+ 4
Here is a way to do it: // Get current date and time and convert it to a string string str = "" + DateTime.Now; // Print it in a file File.WriteAllText("test.bat", str); // Read the file and print its content to be sure it has been done Console.WriteLine(File.ReadAllText("test.bat")); If you want to use a DateTime object to format a file name have a look at this code: https://code.sololearn.com/ch9VB2H81NPd/#cs Hope this help.
5th Dec 2016, 11:21 AM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
0
I used this code: //Formats the like this //FILE_NAME = the string at the begining of the file //{0:yyMMdd} = data format used from user input //.bat = file extension //usertext = variable used to store the user input var data = string.Format("FILE_NAME_{0:yyMMdd}_END.bat", usertext);
8th Dec 2016, 11:59 AM
Mitran Constantin Bogdan
Mitran Constantin Bogdan - avatar