Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Scrabblem · Jan 09, 2019 at 01:25 PM · filesfilestreamwrite data

Writing to a file which i just created.

Hello i have a problem, i created a directory and a file in that directory. And i want to save something to this file but i got a error: IOException: Sharing violation on path. This is my code:

     path_md=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
     path_md = path_md + "/Gladiator";

     Directory.CreateDirectory(path_md);
     path_md = path_md + "/character_stats.txt";
     File.Create(path_md);

     lines = new string[3]; I declared a array above with that: public string[] lines;
     lines[0] = "dadad";
     lines[1] = "adasdasd";
     lines[2] = "adadadadadadad";

     File.WriteAllLines(path_md,lines);
     Debug.Log("after");

The folder and a file are created but i can't write something to it:( Should i close that file or something, i just don't know, instead of a File.WriteAllLines i tried using a StreamWrite but i got the same error:(

Comment
Add comment · Show 1
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 Bunny83 · Jan 09, 2019 at 03:51 PM 0
Share

This question is very similar to this one which was asked just a few days ago. Where do you get the idea from that you have to create the file before you can write to it? "File.Create" opens the file for writing. However you never close the file s$$anonymous$$m that is returned by this method. That's why you get a sharing violation. The File.Create call is completely pointless. "WriteAllLines" does create the file if it doesn't exist.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zapextreme · Jan 09, 2019 at 03:22 PM

You should be able to use StreamWriter For that.

 using(StreamWriter sw = new StreamWriter(fileName, true, System.Text.Encoding.Default))
             {
 
                 string path_md=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
                 path_md = path_md + "/Gladiator";
                 Directory.CreateDirectory(path_md);
                 string [] lines = new string[3];
                 lines[0] = "dadad";
                 lines[1] = "adasdasd";
                 lines[2] = "adadadadadadad";
                 for(int i = 0; i < lines.Length; i++){
                     sw.WriteLine(lines[i]);
                 }
              Debug.Log("after");
             }
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 Scrabblem · Jan 09, 2019 at 03:48 PM 0
Share

It doesn't work for me:( It created a folder in Documents but didn't created a file. A replace a fileName with "/character_stats.txt".

avatar image zapextreme · Jan 09, 2019 at 04:27 PM 0
Share
         using (FileStream fs = File.Create(directoryTarget + "/name.extension"))
         {
             //Do fs stuff here
         }
avatar image Bunny83 · Jan 10, 2019 at 02:18 AM 0
Share

It's pretty pointless to create the directory you want to store the file in after you created the file (aka inside the using block).

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.

Follow this Question

Answers Answers and Comments

99 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 avatar image avatar image avatar image avatar image

Related Questions

Unity does not finish write file (filecontent suddenly ends) 1 Answer

Saving File from "RAM" string array only saving one file 0 Answers

How to handle frame rate drop 1 Answer

Decompress a file with code. 2 Answers

How to create and write a text file after compile/build 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