Shadowtail
Shadowtail

Reputation: 180

IOException: Sharing Violation when attempting to modify a file in the Assets folder

In one of my controllers (built for controlling time), I have the code set to save the current time of day as an integer between 0 and 2 in a text file in the Assets folder. However, in runtime when it tries to execute, it throws IOException: Sharing violation when I try to do so. Code:

 File.Create(Application.dataPath + @"\Variables\TimeOfDay.txt");
 File.WriteAllText(Application.dataPath + @"\Variables\TimeOfDay.txt", timeOfDay.ToString());

Upvotes: 0

Views: 306

Answers (1)

mr.freeze
mr.freeze

Reputation: 14062

You don't need to call File.Create. File.WriteAllText will create the file if it doesn't exist.

File.Create returns a file stream which prevents your next call to File.WriteAllText from accessing it and throws the sharing violation error.

Upvotes: 3

Related Questions