none
File got corrupted whenever abrupt shutdown occur RRS feed

  • Question

  • Hi,

    I want to write some information into a file while my program is running. This information will be used in the current execution itself. My program contains multiple components will write some information to this particular file.
    Mainly last send time, last executed time, etc. It's working fine but whenever force/abrupt power shutdown happened my file got corrupt. Then onward my application will not work. Initially, I tried with .net config files and now I tried for the XML approach, but the issue still persists.Can anyone suggest a solution to fix my problem?

    The files are stored in "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config" this location.


    Coding.....................................


    • Edited by La07K Friday, November 8, 2019 8:42 AM
    Friday, November 8, 2019 8:38 AM

Answers

  • "It's working fine but whenever force/abrupt power shutdown happened my file got corrupt."

    There is nothing you can do about an abrupt power outage. Windows itself has some incredibly small amount of time to react and your code isn't going to be executing. If power outages are common then the correct solution is to use a UPS. This is a hardware problem, not a software one. 

    If you are building an app that absolutely cannot handle corrupt files then you have to build this into your apps. Option 1 (used by VS, Word, etc) is to create recovery files periodically. When your app starts if you detect the current file is corrupt you revert to the last recovery file. You've lost data but your app works. Alternatively (used in video games) is to always write to a temp file. Once the temp file is saved then swap the temp file for the real file using rename. If your app starts up and the current file is corrupt then you can use the temp file you had last. Worst case you lost the changes since the last time you swapped files.

    NEVER modify the config file under the Windows folder. This is a system file and is not for you to muck with. It will be overwritten (potentially) whenever the framework is updated. Your config changes go in your config file only.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by La07K Friday, November 15, 2019 7:49 AM
    Friday, November 8, 2019 2:11 PM
    Moderator

All replies

  • Without seeing your code? Ain't no mind reader..

    But the solution is simple: You need to write to disk. Files in .Net are high level construct, sometimes buffer in memory. The keyphrase is flush to disk.

    Friday, November 8, 2019 9:54 AM
  • In my program first we will write into cache then write to disk.Then i tried directly disk also that time the occurence of file corruption got reduced.But still issue is there

    Coding.....................................

    Friday, November 8, 2019 12:27 PM

  • I want to write some information into a file while my program is running. This information will be used in the current execution itself. My program contains multiple components will write some information to this particular file.
    Mainly last send time, last executed time, etc. It's working fine but whenever force/abrupt power shutdown happened my file got corrupt. Then onward my application will not work. 

    Keep in mind that there are two (at least) occurrences of output file data 
    buffering: (1) the program's file handling buffers, and (2) the operating
    system's disk write buffers. Usually flushing file output results in releasing
    data from the file handling code buffers, which then goes to the OS write buffers
    (cache).

    In C (and C++) code, Microsoft provides a method to force the flushing of buffers
    all the way to the disk:

    Commit-To-Disk Constants
    https://docs.microsoft.com/en-us/cpp/c-runtime-library/commit-to-disk-constants?view=vs-2019

    I haven't dug into the .NET file streams enough to see if using flushing only
    flushes the program's buffers, or if it flushes Windows' write cache as well
    all the way to the disk.

    In Windows you can have write caching enabled (normal setting for hard disks)
    or disabled. See the pics following.

    Drives may also have some internal buffering.

    While using "commit to disk", flushing, and other methods will reduce the 
    probability of data loss in the event of a power outage, loss may still
    occur if power is lost while committing data to the disk is happening.
    To prevent this you really have to have a UPS and associated software
    to force an orderly and prompt shutdown when power goes off.

    - Wayne

    Friday, November 8, 2019 12:31 PM
  • "It's working fine but whenever force/abrupt power shutdown happened my file got corrupt."

    There is nothing you can do about an abrupt power outage. Windows itself has some incredibly small amount of time to react and your code isn't going to be executing. If power outages are common then the correct solution is to use a UPS. This is a hardware problem, not a software one. 

    If you are building an app that absolutely cannot handle corrupt files then you have to build this into your apps. Option 1 (used by VS, Word, etc) is to create recovery files periodically. When your app starts if you detect the current file is corrupt you revert to the last recovery file. You've lost data but your app works. Alternatively (used in video games) is to always write to a temp file. Once the temp file is saved then swap the temp file for the real file using rename. If your app starts up and the current file is corrupt then you can use the temp file you had last. Worst case you lost the changes since the last time you swapped files.

    NEVER modify the config file under the Windows folder. This is a system file and is not for you to muck with. It will be overwritten (potentially) whenever the framework is updated. Your config changes go in your config file only.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by La07K Friday, November 15, 2019 7:49 AM
    Friday, November 8, 2019 2:11 PM
    Moderator
  • Thanks Michael Taylor....the solution worked for me.

    Coding.....................................

    Friday, November 15, 2019 7:48 AM
  • Thanks for your input...it helped me to explore diffrent things.

    Coding.....................................

    Friday, November 15, 2019 7:49 AM