Search code examples
c#file-attributes

File attribute flags is controlled by code or OS?


I am learning about the file attribute enum and have a two questions which I can't find a concrete answer to (although I have my own opinion).

My application checks to see if the Archive flag has been set or not. During my testing, I create a .txt file on my desktop, and removed the flag to archive. I then opened the file, added a line of text and saved the file. The attribute Archive was re-set. This is logical and desirable.

My first question is, what is setting the flag, the program or the OS?

My second question is, should I ever be setting the flags or for the most part can I rely on the OS (assuming that the OS is the thing which sets the flags).


Solution

  • You can control the archive file attribute flag using the ATTRIB command line or the underlying Windows API (e.g. allowing "your code" full control of the flag). However, when a file is modified the archive flag is set. That is not something you have to do in your application or code. The expected usage scenario is this:

    • The operating system will set the flag when a file is created or modified

    • The backup application (e.g. "your code") will reset the flag when a file has been backed up

    However, relying on the archive flag for backup can be problematic because multiple independent backup applications may be using the flag.