Problems with binary files at power failure: Windows 10 IoT Enterprise, code VB.NET, Visual Studio 2017

Anonym
2019-11-13T13:41:59+00:00

Hi,

I have problems with power failure for a machine. I save data to binary files, even having a backup file with the same data.

The situation is that when I run the programme I can see that the data.stn contains values. Then I shut it down, and sometimes it works fine but then sometimes it tells me that the binary stream is out of order. When I check with file explorer both files data.stn and BUdata.stn are there but only the binary header is in the file, rest has no data. This means that the programme will not work.  

        StringFile = "C:\Users\Public\data.stn"

        BUStringFile = "C:\Users\Public\BUdata.stn"

        Dim f As New FileStream(StringFile, IO.FileMode.Create)

        Dim fo As New Runtime.Serialization.Formatters.Binary.BinaryFormatter

        Try

            fo.Serialize(f, Me.newMainData.Data)

            …...

            f.Flush()

            f.Close()

        Catch ex As Exception

            f.Close()

            EventLog("Exception when saving file data.stn." & ex.ToString)

        End Try

            ' Make a BackUp copy of the indata file. To be used in case the

            ' original file gets corrupt. The same procedure for BUdata.stn repeated.

First of all, I read that I had to disable windows writecache policy as it requires a constant access to power. From the beginning I only had f.close() but added f.flush() as I found that this shall write the data directly to the disk without going via any cache!? And both these actions did make the frequency of error to go down. 

(This is a development of the programme that originally was written for Windows XP Embedded and there we have not experienced any problems of this kind. Frequent power failures but always came up again.)

The hard disk also have a 60 ms delay from when the power failure occurs that should be enough for the flush to work. But obviously it doesn't. Further I can't understand why both files are empty after the restart?

What do I do wrong? Help would be most welcome.

Best Regards,

Per

Windows för hemmet | Windows 10 | Filer, mappar och lagring

Låst fråga. Den här frågan har migrerats från Microsoft Support Community. Du kan rösta på om det är till hjälp, men du kan inte lägga till kommentarer eller svar eller följa frågan.

0 kommentarer Inga kommentarer

2 svar

Sortera efter: Mest användbara
  1. Anonym
    2019-11-13T14:34:58+00:00

    Thanks! I perceive it as a windows file problem. The way windows save files.

    That was why I put the question here.

    But alright I can copy the question there.

    Best wishes, Per

    .

    Var det här svaret till hjälp?

    0 kommentarer Inga kommentarer
  2. DaveM121 918.5K Ryktespoäng Oberoende rådgivare
    2019-11-13T13:45:06+00:00

    Hi Per

    Community is just a consumer forum, due to the scope of your question (VB coding in VS) can you please post this question to our sister forum on MSDN (the Developer Network) in the VB section (linked below)

    Over there you will have access to a host of VB coders and VS experts and will get a knowledgeable and quick answer to this question . ..

    https://social.msdn.microsoft.com/Forums/vstudi...

    Var det här svaret till hjälp?

    0 kommentarer Inga kommentarer