Hi! I am making a game in Unity with c# as my main programming language. I try to make a new save.txt and autosave.txt if it doesn’t already exist in the app folder. It can create it, but it doesn’t work properly. Here are my codes:
Creating and writing a new save:
void Start () {
if(!File.Exists(Application.dataPath.ToString() + "/Save.txt"))
{
File.CreateText(Application.dataPath.ToString() + @"/Save.txt");
saveFilePath = Application.dataPath.ToString() + @"/Save.txt";
TextWriter writer = new StreamWriter(saveFilePath, false);
writer.WriteLine("10:21:59", "13 / 06 / 2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500","2","1","1","5000", "10");
writer.Close();
}
if(!File.Exists(Application.dataPath.ToString() + "/AutoSave.txt"))
{
File.CreateText(Application.dataPath.ToString() + @"/Autosave.txt");
saveFilePath = Application.dataPath.ToString() + @"/Autosave.txt";
TextWriter writer = new StreamWriter(saveFilePath, false);
writer.WriteLine("00:00:00", "01 / 01 / 2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500", "2", "1", "1", "5000", "10");
writer.Close();
}
}
Here is my writing an existent .txt code:
public void OnSaveGame()
{
saveFilePath = Application.dataPath.ToString() + @"/Save.txt";
isNewGame = false;
TextWriter writer = new StreamWriter(saveFilePath, false);
theTime = System.DateTime.Now.ToString("hh:mm:ss"); theDate = System.DateTime.Now.ToString("dd/MM/yyyy");
string ZeroPart = theTime + "," + theDate + ",";
string FirstPart = income;
writer.WriteLine(ZeroPart + FirstPart);
writer.Close();
SavingPanel.SetActive(true);
Invoke("WaitTime",2);
writer.Close();
}
I don’t know what I do wrong.
P.S. In unity,if it helps, when I run it, it says that it “IOException: Sharing violation on path C:*\Assets\Save.txt”