I would like to write to a read-only file using XmlWriter
. What is the proper way to do this?
Currently I have:
XmlWriterSettings lSettings = new XmlWriterSettings();
lSettings.Encoding = new UTF8Encoding(false);
lSettings.Indent = true;
XmlWriter lWriter = XmlWriter.Create(lPath, lSettings);
lXml.WriteContentTo(lWriter);
lWriter.Close();
I'm wondering if there's a way to avoid clearing and re-setting the read-only flag.
If a file is set as read-only, you do need to re-set the flag, no workarounds.
Of course, you could delete the file (which still requires you to clear the flag) and write a new one, but then risk that if your write operation doesn't happen (exception somewhere), you lose the contents.