Create new text file everytime the game runs
Hey all,
I was wondering if there was a way to create a new .txt file every time the game runs? I am able to write the data I want to a .txt file but it always adds onto the same file when I play my game. What I am looking for is when ever I start the game it will create a new .txt file and save all that data for that instance only.
Here is my current code:
void addItemsToLog()
{
File.AppendAllText("OutputLogs/outputLog.txt", "\r\n" + "\r\n" + "Player 1 Stats: ");
File.AppendAllText("OutputLogs/outputLog.txt", "\r\n" + "Player 1 Money: $" + player1Money);
foreach (string property in player1PropertyOwned)
{
File.AppendAllText("OutputLogs/outputLog.txt", "\r\n" + "Player 1 owns: " + property);
//yield return null;
}
File.AppendAllText("OutputLogs/outputLog.txt","\r\n" + "\r\n" + "Player 2 Stats: ");
File.AppendAllText("OutputLogs/outputLog.txt", "\r\n" + "Player 2 Money: $" + player2Money);
foreach (string property in player2PropertyOwned)
{
File.AppendAllText("OutputLogs/outputLog.txt", "\r\n" + "Player 2 owns: " + property);
}
}
This will give me all the data in a file named "outputlog.txt" but what I am trying to do is when I run the game a second time it will save that to "outputlog(1).txt" for example.
Thanks in advance.
Answer by adriono · Aug 02, 2016 at 04:05 PM
Write to some variable (string startTime
) value from DateTime.Now
(System
namespace) when application is starting and after that you can save data to file "outputLog_"+startTime+".txt"
Thanks for your reply, so I did what you said but I was getting a problem with the format as it was using "/" so it was constantly trying to change directory. I managed to get it working by changing the format of the date and time that was being retrieved if anyone else is interested below is a copy of my final code:
//Set up new log .txt file every time the game is played
private System.DateTime startGame;
private string startGameString;
void Start()
{
//Find the date and time when the game was run
startGame = System.DateTime.Now;
startGameString = startGame.ToString("dd-mm-yyyy-hh-mm-ss");
}
void addItemsToLog()
{
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "\r\n" + "Player 1 Stats: ");
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "Player 1 $$anonymous$$oney: $" + player1$$anonymous$$oney);
foreach (string property in player1PropertyOwned)
{
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "Player 1 owns: " + property);
//yield return null;
}
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "\r\n" + "Player 2 Stats: ");
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "Player 2 $$anonymous$$oney: $" + player2$$anonymous$$oney);
foreach (string property in player2PropertyOwned)
{
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n" + "Player 2 owns: " + property);
}
File.AppendAllText("OutputLogs/" + "outputlog" + startGameString + ".txt", "\r\n");
gameOverPanel.SetActive(true);
}