How can C # change a file attribute while preserving the value of its other attributes?

File.SetAttributes(file_path, FileAttributes.ReadOnly); 

So the file will have the ReadOnly attribute set and all others will disappear.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Like this:

 File.SetAttributes(file_path, File.GetAttributes(file_path) | FileAttributes.ReadOnly); 

Or so:

 new FileInfo(file_path).Attributes |= FileAttributes.Hidden; 
  • Thanks, now everything works. - Roman
  • @Roman if the answer helped you, then mark it with an arrow up. If you decide your question, then put a green checkbox. No need to thank for comments. - Kromster