System.IO.IOException: Sharing violation on path in C#

前端 未结 2 1348
星月不相逢
星月不相逢 2021-01-26 15:45

I have used below code to save the file in xamarin forms ios but give me

System.IO.IOException: Sharing violation

please help me.

2条回答
  •  醉话见心
    2021-01-26 16:15

    Have you checked that your app has permission for accessing files on the system? Also i would to a check to see if the files exist before opening it with File.Exists();.

    Also there are better ways of reading and writing to files in C#:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    string filename = Path.Combine(path, "myfile.txt");
    
    using (var streamWriter = new StreamWriter(filename, true))
    {
        streamWriter.WriteLine(DateTime.UtcNow);
    }
    
    using (var streamReader = new StreamReader(filename))
    {
        string content = streamReader.ReadToEnd();
        System.Diagnostics.Debug.WriteLine(content);
    }
    

提交回复
热议问题