Sharing violation on path

Hi guys,
i have a problem with WriteAllText
it always says: Sharing violation on path /data/data/aeMVe.filme_schauen/files/ip_addrs.aMVe

but a few hours before, exactly this code worked like a charm, but now nothing is working, here is the whole code:

            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            Setts [0] = path + "/ip_addrs.aMVe";
            Setts [1] = path + "/eco_pt.aMVe";
            if (File.Exists(Setts [0]) == false)
                File.Create(Setts[0]);
            if (File.Exists(Setts [1]) == false)
                File.Create(Setts[1]);
            try
            {           
                WebClient client = new WebClient ();
                n_infos[0] = DecodeFrom64(client.DownloadString ("http://**censored**/Android/aeMVe/Strings/IP-Adress.aMVe"));
                n_infos[1] = DecodeFrom64(client.DownloadString ("http://**censored**/Android/aeMVe/Strings/Eco_rq-Path.aMVe"));
                File.WriteAllText(Setts[0], n_infos[0]);
                File.WriteAllText(Setts[1], n_infos[1]);
            } catch {
                Toast.MakeText (this, "Sync failed, using old data.", ToastLength.Short).Show ();
                n_infos [0] = File.ReadAllText (Setts [0]);
                n_infos [1] = File.ReadAllText (Setts [1]);
            }

Answers

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    Hi @Miracollix‌ not sure if any help but I had a similar 'permissions' problem buried in this thread that I didn't solve and worked around, is yours on all devices as mine was hit and miss ?

    http://forums.xamarin.com/discussion/comment/62359/

    Happy to poke more to solve as annoying

  • EliasKhairhoumEliasKhairhoum DEMember ✭✭

    @AndyFlisher‌ i still dont know how to fix it, i also worked around it

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    Cheers @Miracollix‌ I'll save this for the mythical day when I have some spare time and have another fight / file a bug report

  • SuchithSuchith INMember ✭✭✭

    even i got the same error("Sharing violation on path") in the following code...

    if ( !File.Exists ( Path.Combine ( strCacheFolderPath , "Extras" , "abc.xml" ) ) )
    {
    objCommonFunctions.fnAlertMsg ( "xyz" );
    return false;
    }
    dsContract.ReadXml ( Path.Combine ( strCacheFolderPath , "Extras" , "abc.xml" ) );

    Included the else part and that got worked for me, as follows..

    if ( !File.Exists ( Path.Combine ( strCacheFolderPath , "Extras" , "abc.xml" ) ) )
    {
    objCommonFunctions.fnAlertMsg ( "xyz");
    return false;
    }
    else
    {
    dsContract.ReadXml ( Path.Combine ( strCacheFolderPath , "Extras" , "abc.xml" ) );
    }

  • To decide this problem you can wrap your code using "lock()". Just add first in your code:
    1) private static object locker = new object();
    2) lock(locker)
    {
    /* your code */
    }

    In my case I had this exception when different threads tried to write file on the disk at the same time.

  • JedatJedat SIMember ✭✭
    edited May 2017

    One workaround is to access the "Result" property of the task, which will cause your thread to block until the result is available:

    var x = YourAsyncMethod().Result;

  • JedatJedat SIMember ✭✭

    @Jedat said:
    One workaround is to access the "Result" property of the task, which will cause your thread to block until the result is available:

    var x = YourAsyncMethod().Result;

    Never mind. Don't use the Result property (as soon as you are accessing the same file multiple times, the Result property will get you in trouble). Just do async all the way. Everything async and everything should work fine with PCLStorage.

  • PrinceTegatonPrinceTegaton USMember ✭✭
    edited August 2017

    This is caused when you try to Read or Write a file you just created from a separate stream. Solving this is very simple, just dispose the filestream you used in creating it and then you can access the file freely.

    if (!File.Exists(myfile))
    {
           var fs = new FileStream(fav, FileMode.Create);
           fs.Dispose();
           string text = File.ReadAllText(myfile);
    }
    
  • DavyHomesDavyHomes Member ✭✭

    @PrinceTegaton said:
    This is caused when you try to Read or Write a file you just created from a separate stream. Solving this is very simple, just dispose the filestream you used in creating it and then you can access the file freely.

    if (!File.Exists(myfile))
    {
           var fs = new FileStream(fav, FileMode.Create);
           fs.Dispose();
           string text = File.ReadAllText(myfile);
    }
    

    Thank you sir, you saved my day !

  • DipenHansawalaDipenHansawala USMember ✭✭

    Because of File.ReadAllText(myfile); line error is occurred.
    Do i have used bellow mthod and it works

    public byte[] ReadAllBytes(string fileName)
    {
    byte[] buffer = null;
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
    buffer = new byte[fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
    }
    return buffer;
    }

Sign In or Register to comment.