1

I have an xml file that has different marks in it that i need to update and need to pick up. this mark is from an api and is used so i only get new data. however when i try to write away the data or read the file it get locks all the time. these are the 2 functions that i use to write or read from the file.

    private void SetMark(string name, string mark)
    {
        var marksfile = (string)_appSettings.GetValue("MarksFile", typeof(string));

            _marks = new dsMarks();
            try
            {
                if (File.Exists(marksfile))
                {
                    using (var reader = new StreamReader(marksfile))
                    {
                        _marks.ReadXml(reader);
                    }
                }
            }
            catch (Exception)
            {
                _marks = null;
                throw;
            }

        var row = _marks.Mark.FindByName(name);
        row.TimeMark = mark;
        _marks.AcceptChanges();
        using (var writer = new StreamWriter(marksfile))
        {
            _marks.WriteXml(writer);
        }
    }

    private string GetMark(string name)
    {
        var marksfile = (string)_appSettings.GetValue("MarksFile", typeof(string));
            _marks = new dsMarks();
            try
            {
                if (File.Exists(marksfile))
                {
                    using (var reader = new StreamReader(marksfile))
                    {
                        _marks.ReadXml(reader);
                    }

                }
            }
            catch (Exception)
            {
                _marks = null;
                throw;
            }
        var row = _marks.Mark.FindByName(name);
        var mark = row.TimeMark;

        return mark;
    }
Bjorn Fontaine
  • 426
  • 1
  • 4
  • 17
  • [look at this question](https://stackoverflow.com/questions/12478512/how-to-open-a-streamreader-in-sharedenywrite-mode). It answer it. – Lior Oct 01 '18 at 07:21
  • Its all in the documentation for a file stream https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.-ctor?view=netframework-4.7.2 – TheGeneral Oct 01 '18 at 07:22
  • Is it possible that your program to have mutiple write/read operations ongoing at the same time? – Magnus Oct 01 '18 at 08:27

2 Answers2

0

You might want to use FileStream instead of StreamReader as the former locks the file from other accessors. FileStream is better for read sharing.

private string GetTrimbleMark(string name)
{
    var marksfile = (string)_appSettings.GetValue("MarksFile", typeof(string));
        _marks = new dsMarks();
        try
        {
            if (File.Exists(marksfile))
            {
                using (var reader = new FileStream(marksfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    _marks.ReadXml(reader);
                }

            }
        }
        catch (Exception)
        {
            _marks = null;
            throw;
        }
    var row = _marks.Mark.FindByName(name);
    var mark = row.TimeMark;

    return mark;
}
Allan Chua
  • 9,305
  • 9
  • 41
  • 61
0

I 'll add fileAccess before openning my streamreader

            if (File.Exists(marksfile))
            {
                FileStream fs = new FileStream(marksfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                using (var reader = new StreamReader(fs))
                {
                    _marks.ReadXml(reader);
                }
            }
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • it worked once but then for some reason my file was empty and had no lines anymore. this is the only program that is accessing the file and these are the only methodes that handles the file – Bjorn Fontaine Oct 01 '18 at 07:59
  • ok but you could have a write operation in same time than a read operation on the file? – Frenchy Oct 01 '18 at 08:16