Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question "Exception:Sharing violation on path" When antivirus software is running in the background

Discussion in 'Scripting' started by sun201801010, May 24, 2023.

  1. sun201801010

    sun201801010

    Joined:
    Dec 26, 2018
    Posts:
    8
    I am trying to implement the pause/resume download function.
    It work find if turn off the antivirus software.
    But i cannot demand that all users turn off their antivirus software.
    It only happen after called StopDownload then call StartDownload again.
    So is there any other solution?


    using System;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Networking;

    public class SelfDownloadHandler : DownloadHandlerScript
    {
    public long downloadedLength;
    FileStream fileStream ;
    public SelfDownloadHandler(string savePath) : base(new byte[1024 * 200])
    {
    try
    {
    fileStream = new FileStream(savePath, FileMode.Append, FIleAccess.Write);
    downloadedLength = fileStream.Length;
    }
    catch(Exception e)
    {
    //"Sharing violation on path" Exception catched here
    Debug.LogError("open file exception:" + e);
    }
    }

    //call every frame
    protected override bool ReceiveData(byte[] data, int dataLength)
    {
    if (data == null || data.Length == 0)
    {
    Debug.Log("download failed");
    return false;
    }
    fileStream?.Write(data, 0, dataLength);
    downloadedLength += dataLength;
    return true;
    }

    public void Close()
    {
    if (fileStream != null)
    {
    fileStream.Close();
    fileStream.Dispose();
    fileStream = null;
    Debug.Log("close filestream");
    }

    Dispose();
    }
    }

    public class TestDownload : MonoBehaviour
    {
    public string downloadUrl;
    public string savePath;
    UnityWebRequest unityWebRequest;

    void Start()
    {

    }

    IEnumerator StartDownload()
    {
    SelfDownloadHandler downloadHandler = new SelfDownloadHandler(savePath);

    unityWebRequest = UnityWebRequest.Get(downloadUrl)
    unityWebRequest.SetRequestHeader("Range", "bytes=" + downloadHandler.downloadedLength + "-");
    unityWebRequest.downloadHandler = downloadHandler;
    yield return unityWebRequest.SendWebRequest();
    CloseWebRequest();
    }

    void StopDownload()
    {
    if (unityWebRequest != null)
    {
    unityWebRequest .Abort();
    CloseWebRequest();
    }
    }

    void CloseWebRequest()
    {
    if (unityWebRequest != null)
    {
    ((SelfDownloadHandler)unityWebRequest .downloadHandler).Close();
    unityWebRequest .Dispose();
    unityWebRequest = null;
    }
    }

    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    StartCoroutine(StartDownload());
    }
    else if(Input.GetKeyDown(KeyCode.Tag))
    {
    StopDownload();
    }
    }
    }
     
  2. sun201801010

    sun201801010

    Joined:
    Dec 26, 2018
    Posts:
    8
    Testing with other downloaded software does not have this issue