- Home /
When writing a txt file, it says sharing violation
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"
Answer by Dave-Carlile · Jun 14, 2017 at 12:13 PM
Application.dataPath
is the path to the application data folder. This is the folder where the game is installed, and your game isn't going to have permissions to write there. If you want a place to be able to write persistent data then use Application.persistentDataPath.
The sharing violation error is actually kind of a combination of "sharing violation, access denied, file in use". In your case it's most likely "access denied".
Answer by cgarossi · Jun 14, 2017 at 01:14 PM
You'll need to dispose the writer after you've written it away. writer.Dispose();
This releases the file handles and allows it to be opened again.
A better way is to use the dispose/using pattern:
using(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();
}
This will ensure the file is properly closed when you're finished with it.
@cgarossi what is really strange is the fact that when save.txt and autosave.txt are in place and with content, the program runs without a problem, but when it has to create the text and write in it, it doesn't work anymore
Answer by OctavMatei · Jun 14, 2017 at 08:07 PM
@cgarossi and @Dave-Carlile this is the modification:
public void CreateFileTextSave()
{
if (!File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
{
File.CreateText(Application.persistentDataPath.ToString() + @"/Save.txt");
saveFilePath = Application.persistentDataPath.ToString() + @"/Save.txt";
using (TextWriter writer = new StreamWriter(saveFilePath, false))
{
writer.Write("00:00:00" + "01 / 01 / 2017" + "1" + "1" + "-21" + "20000" + "100" + "500" + "50" + "20" + "500" + "2" + "1" + "1" + "5000" + "10");
writer.Close();
}
}
}
public void CreateFileTextAutoSave()
{
if (!File.Exists(Application.persistentDataPath.ToString() + @"/Autosave.txt"))
{
File.CreateText(Application.persistentDataPath.ToString() + @"/Autosave.txt");
saveFilePath = Application.persistentDataPath.ToString() + @"/Autosave.txt";
using (TextWriter writer = new StreamWriter(saveFilePath, false))
{
writer.Write("00:00:00" + "01 / 01 / 2017" + "1" + "1" + "-21" + "20000" + "100" + "500" + "50" + "20" + "500" + "2" + "1" + "1" + "5000" + "10");
writer.Close();
}
}
}
public void Start()
{
filePaths = new string[2] { @"/Autosave.txt", @"/Save.txt" };
if (File.Exists(Application.persistentDataPath.ToString() + @"/Autosave.txt") && File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
{
//Code
}
if (!File.Exists(Application.persistentDataPath.ToString() + @"/Save.txt"))
CreateFileTextSave();
if (!File.Exists(Application.persistentDataPath.ToString() + @"Autosave.txt"))
CreateFileTextAutoSave();
}
but it still writes this error:
IOException: Sharing violation on path C:\Users\USER\AppData\LocalLow*\Autosave.txt System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124) System.IO.StreamWriter..ctor (System.String path, Boolean append) (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool) MainMenuBasicBehaviour.CreateFileTextAutoSave () (at Assets/Scripts/MainMenuBasicBehaviour.cs:49) MainMenuBasicBehaviour.Start () (at Assets/Scripts/MainMenuBasicBehaviour.cs:67)
Line 49 is using (TextWriter writer = new StreamWriter(saveFilePath, false))
Line 67 is CreateFileTextAutoSave();
It also gives an error at Line 34 is using (TextWriter writer = new StreamWriter(saveFilePath, false))
Line 65 is CreateFileTextSave();
It creates the files, but they are empty.
Have you tried using File.ReadAllText and File.WriteAllText? Those should simplify your code quite a bit.
$$anonymous$$ost likely you're not building a path correctly somewhere, or you're trying to create the file multiple times or something. From the code you're showing here it looks like the aforementioned functions should work as replacements.
@Dave-Carlile It works perfectly when I only try to rewrite Save.txt and Autosave.txt, but when I try to create them, it doesn't work anymore
Your answer
Follow this Question
Related Questions
[Closed] Place to store user data 1 Answer
How do I make a basic easy Save and load function with JavaScript 1 Answer
Saving Player Data 3 Answers
Why does PlayerPrefs save to the Windows Registry? 0 Answers
Loading a saved Game 1 Answer