C# make file read/write from readonly

Thomas Clayson picture Thomas Clayson · Nov 10, 2011 · Viewed 31.4k times · Source

If File.SetAttributes("C:\\myFile.txt", FileAttributes.ReadOnly); sets a file as read only, how do I set it back to read/write if I need to?

I suspect it would be FileAttributes.Normal however will this change any other properties of the file? There isn't an awfully descriptive note on the MSDN site...

The file is normal and has no other attributes set. This attribute is valid only if used alone.

Thanks

Answer

matt picture matt · Nov 10, 2011

To remove just the ReadOnly attribute, you'd do something like this:

File.SetAttributes("C:\\myfile.txt", File.GetAttributes("C:\\myfile.txt") & ~FileAttributes.ReadOnly);

This will remove the ReadOnly attribute, but preserve any other attributes that already exist on the file.