• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OctavMatei · Jun 14, 2017 at 08:30 AM · gamefilesavingsave data

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"

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OctavMatei · Jun 15, 2017 at 10:20 AM 0
Share

@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

avatar image
0

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();

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OctavMatei · Jun 14, 2017 at 08:11 PM 0
Share

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.

avatar image Dave-Carlile · Jun 14, 2017 at 08:19 PM 0
Share

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.

avatar image OctavMatei Dave-Carlile · Jun 15, 2017 at 09:53 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Closed] Place to store user data 1 Answer

Problem when saving and loading 1 Answer

Sharing Violation On Path 1 Answer

PlayerPrefs not saving in build 1 Answer

Tile map placement and saving in game? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges