x


Sharing violation on path

Hey,

I am creating a file, where I can store the game quality settings.

Here is the code:

 function Start()
 {
     readConfig();
 }
 
 function OnGUI()
 {
     //Lots of code
     
     if (GUI.Button(Rect (150, 640, 150, 50), "Back", myStyle))
     {
         playSound();
         UpdateConfig();
         readConfig();
         SetResolution();
     }
     
     //Lots of code
 }
 
 function readConfig()
 {
     var ind : int = 0;
     sr = new File.OpenText(configPath);
     input = "";
 
     while (true)
     {
         input = sr.ReadLine();
         if (input == null) 
         {
             break;
         }
         config.Push(input);
     }
     
     audioVolume = parseInt(config[5]);
     musicVolume = parseInt(config[6]);
     
     SetResolution();
 }
 
 function SetResolution()
 {
     var res     : int = parseInt(config[2]);
     var full     : int = parseInt(config[3]);
     
     var isFull    : boolean;
     
     switch (full)
     {
         case 0:
             isFull = false;
             break;
         
         case 1:
             isFull = true;
             break;
             
         default:
             break;
     }
     
     switch (res)
     {
         case 0:
             Screen.SetResolution (800, 600, isFull);
             break;
             
         case 1:
             Screen.SetResolution (1024, 768, isFull);
             break;
             
         case 2:
             Screen.SetResolution (1280, 768, isFull);
             break;
             
         case 3:
             Screen.SetResolution (1366, 768, isFull);
             break;
         
         default:
             break;
     }
     
     QualitySettings.currentLevel = QualityLevel.Fantastic;
     
 }
 
 function UpdateConfig()
 {
     var sw : StreamWriter = new StreamWriter(configPath);
     for (var i = 0; i < config.length; i++)
     {
         sw.WriteLine(config[i]);
     }
     sw.Flush();
     sw.Close();
 }

The problem is, when I click on the back button, I get a "IOException: Sharing violation on path D:UnityMarble Worlds 2Marble Worldsconfig.txt". The file is created succesfully by another script, and it is there. Also, when I make a build from the game, the file is not rewrited. It writes new lines to the end of the file, rather than deleting the previous lines, and write the new line's to their place.

What is wrong with the code?

Thanks for your help.

more ▼

asked Nov 18, 2011 at 10:33 AM

Dreeka gravatar image

Dreeka
176 172 134 136

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Should probably Close sr in readConfig. That would cause an error when you tried to open to write it (can't write to a file if someone is reading.)

new StreamWriter(...) often has an optional 2nd input specifying Append (write to end) or write from start. The default is usually to erase and rewrite, but maybe not in unityScript.

more ▼

answered Nov 19, 2011 at 05:59 AM

Owen Reynolds gravatar image

Owen Reynolds
22.9k 3 16 76

sr.Close() solved my first problem, thanks for that.

I have looked at the properties of SteamWriter, if if I use new StreamWriter(configPath, false); it should rewrite the file, but it is still not working. The strange this is, that I have used this method on an another file, and it is working for that file. I dont know what is causing this problem for the config file.

Edit: I have found out, that it isn't writing it to the end of the file, it is writing it to the begining of the file.

Nov 19, 2011 at 10:54 AM Dreeka

Quick test on config.Length? Maybe the real problem is that your config data is growing?

Nov 19, 2011 at 05:02 PM Owen Reynolds

You are right, the length is doubled when I use the back button. Now I have to find out where. Thanks for the tip :)

Edit: I have found out where. I call ReadConfig every time when I use the back button, and I am not clearing it out before reading the file again.

Nov 19, 2011 at 05:21 PM Dreeka
(comments are locked)
10|3000 characters needed characters left

How to make a file and write to it in the same function, without getting the file share violation error. (I'm not sure if this is what Owen Reynolds meant, maybe i'm just saying the same thing)

 StreamWriter sw = System.IO.File.CreateText("path");
 sw.Close();
 System.IO.File.WriteAllText("path", "text");
more ▼

answered Sep 26 at 10:16 PM

Skroefkop gravatar image

Skroefkop
1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x359
x10

asked: Nov 18, 2011 at 10:33 AM

Seen: 8044 times

Last Updated: Sep 26 at 10:16 PM