not_a_commie said:
I'm calling XmlDocument.Save with a filename that often varies. It is
sometimes absolute path and sometimes relative path. Occasionally I'll
get a re-entrant save to the same filename, at which point
XmlDocument.Save throws an exception.
See below.
What is the right way to lock on a filename that may or may not be
relative?
The file name being a relative or absolute path is completely irrelevant.
Or is there a way to make XmlDocument.Save wait until the file is
available for writing?
It sounds like from what you wrote that you have potentially two calls
attempting to truncante and write data to the same file at the same time.
Which is a bad idea to begin with but...
XmlDocument.Save with a filename attempts to open the file in Create mode
(either CreateNew or Truncate if the file exists) for Writing with only Read
sharing. This means that until the Save function returns, nothing else can
write to that file.
if you don't really care that the second simultaneous Save is going to
destroy the data from the first Save, you could just do a lock the
XmlDocument.Save function call, which will enforce only one save at a time,
or you could do a bit more complicated logic if you do care that you are
overwriting an existing file, which will happen once the first Save()
completes.
Note, don't be tempted to open a FileStream for ReadWrite sharing and use
that overload of Save. You'll end up with a garbage file.
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?