How to remove read-only flag in a file in .NET?

How do you clear the read-only flag in a file in .NET and leave it intact?

+3
source share
2 answers

Can't you just do:

FileInfo f = new FileInfo("yourfile.txt");
f.IsReadOnly = false;

Or am I missing something?

+4
source

I would get an instance of FileInfo for the file and then set the IsReadOnly property to false (as per the documentation here: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.isreadonly.aspx ):

new FileInfo("path").IsReadOnly = false;

If you insist on using the static GetAttributes and SetAttributes methods in the File class, you can simply do this:

File.SetAttributes("path", 
    File.GetAttributes("path") & ~FileAttributes.ReadOnly);

, , - ( FileAttributes.ReadOnly), ( ~), , ( File.GetAttributes( "" )).

+2

Source: https://habr.com/ru/post/1726493/


All Articles