D
D
Dmitry Korolev2019-02-12 22:18:34
.NET
Dmitry Korolev, 2019-02-12 22:18:34

Why is the function reading the file unstable?

The function sometimes does not read the file
How can I fix this?

static public void ReadParam()
        {
            BinaryReader br;
            string path = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName + "\\noticeprogramdata.dat";
            try
            {
                string locale = string.Empty;
                int enume = -1;
                List<ScheduleCell> cells = new List<ScheduleCell>();
                br = new BinaryReader(new FileStream(path, FileMode.OpenOrCreate));
                try
                {
                    newStart = br.ReadBoolean();
                    wD1 = br.ReadBoolean();
                    wD2 = br.ReadBoolean();
                    wD3 = br.ReadBoolean();
                    wD4 = br.ReadBoolean();
                    eD1 = br.ReadBoolean();
                    eD2 = br.ReadBoolean();
                    eD3 = br.ReadBoolean();
                    eD4 = br.ReadBoolean();
                    dayToday = br.ReadByte();
                    DateTimeOffset.TryParse(br.ReadString(), CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeOld);

                    locale = br.ReadString();
                    enume = br.ReadInt32();
                    for (;;)
                    {
                        cells.Add(new ScheduleCell(br.ReadString(), br.ReadInt32()));
                    }
                }
                catch (EndOfStreamException exc)
                {
                    Console.WriteLine(exc.Message.ToString());
                }
                br.Close();
                schedule = new ScheduleCollection(cells, (ScheduleCollection.SortDirectionEnum)enume, locale);
            }
            catch (Exception exc)
            {
                Console.WriteLine(">>>"+exc.Message.ToString());
                if (br != null)
                    br.Close();
            }
        }

I use this code in windows forms project

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2019-02-13
@kttotto

Run the studio as admin and check if the same issue occurs. If not, then it's a matter of rights. If so, look at the file size, buffer, file lock by other processes, perhaps a simultaneous attempt is made to read from other threads.

T
tupovat_bydlovat, 2019-02-19
@tupovat_bydlovat

Ponecropost.
Instead of catching the eof-execution and the for(;;) loop, you can compare the position and length of the stream as a while condition.
And on the question - you catch one single specific exception there (well, or a stack of specific exceptions) and only close the reader in it, while calling a bunch of all kinds of readInt32 and creating along the way a bunch of other objects, the implementation of which can also be lame and possibly throw exceptions.
For good, it is necessary to wrap in 2 using, as advised above. If you really want to catch exceptions, you need to close the reader in finally, as was also advised above, but in order to deal with the problem, start by catching and logging other exceptions, and not just problems with rights and crooked paths.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question