title: Unity IOException: Sharing violation on path for output_log.txt, how to read a file in use?
body: <p>I am getting the following exception in my game build when trying to upload the file to a remote url.</p>
  <pre><code>IOException: Sharing violation on path C:\Users\vipin\AppData\LocalLow\DefaultCompany\ChemOcrypt\output_log.txt
    at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0019e] in &lt;f826c2584fc94ec19a48a6576640bdc5&gt;:0 
    at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in &lt;f826c2584fc94ec19a48a6576640bdc5&gt;:0 
    at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
    at System.IO.File.OpenRead (System.String path) [0x00000] in &lt;f826c2584fc94ec19a48a6576640bdc5&gt;:0 
    at System.IO.File.ReadAllBytes (System.String path) [0x00000] in &lt;f826c2584fc94ec19a48a6576640bdc5&gt;:0 
    at PopulateTest.submitToGoogleSheet () [0x00b99] in &lt;a5e233b1914d4eecaae056ee383cbe4d&gt;:0 
    at PopulateTest.EvaluateScore () [0x00221] in &lt;a5e233b1914d4eecaae056ee383cbe4d&gt;:0 
    at UnityEngine.Events.InvokableCall.Invoke () [0x00011] in &lt;b44fd21d6d27420ebf318fe55aef0978&gt;:0 
    at UnityEngine.Events.UnityEvent.Invoke () [0x00023] in &lt;b44fd21d6d27420ebf318fe55aef0978&gt;:0 
    at UnityEngine.UI.Button.Press () [0x00027] in &lt;b864ecc7b7bb4f98a1934b2a75272d9c&gt;:0 
    at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00011] in &lt;b864ecc7b7bb4f98a1934b2a75272d9c&gt;:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00008] in &lt;b864ecc7b7bb4f98a1934b2a75272d9c&gt;:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00070] in &lt;b864ecc7b7bb4f98a1934b2a75272d9c&gt;:0
  </code></pre>
  <p>The exception occurs because the game is writing the data continuously to output file. How do I read the contents of file in when it is already being used for uploading to a server?</p>
  <p>Below is my piece of code which is responsible for uploading this output_log.txt file.</p>
  <pre><code>string FILEUPLOAD_BASE_URL = GameSession.FILEUPLOAD_BASE_URL;
  string[] files = Directory.GetFiles(Application.persistentDataPath, &quot;output*&quot;);
  if (files.Length &gt; 0)
  {
      UnityEngine.WWWForm outputForm = new WWWForm();
      outputForm.AddField(&quot;key&quot;, &quot;OutputLogsEXP2/${filename}&quot;);
      string fileName = Path.GetFileName(files[0]);
      outputForm.AddBinaryData(&quot;file&quot;, File.ReadAllBytes(files[0]), fileName, &quot;text/plain&quot;);
      StartCoroutine(PostDataToForm(FILEUPLOAD_BASE_URL, outputForm));
  }
  
  IEnumerator PostDataToForm(string url, WWWForm form)
  {
      WWW www = new WWW(url, form);
      float elapsedTime = 0.0f;
      while (!www.isDone)
      {
          elapsedTime += Time.deltaTime;
          //Matrix4x4 wait time is 120s
          if (elapsedTime &gt;= 120f)
          {
              break;
          }
          yield return null;
      }
      if (!www.isDone || !string.IsNullOrEmpty(www.error))
      {
          Debug.LogError(&quot;Connection error while sending analytics from PopulateTest... Error:&quot; + www.error);
          // Error handling here.
          yield break;
      }
  
      if (www.isDone)
      {
          Debug.Log(&quot;Data Sent successfully.&quot;);
          yield break;
      }
  }
  </code></pre>