Re: lock on filename for XmlDocument.Save?



"not_a_commie" <notacommie@xxxxxxxxx> wrote in message news:1189457405.305209.178430@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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?

.



Relevant Pages

  • Re: absolute path to relative path conversion
    ... you give as you are effectively just after the filename: ... You could walk from you absolute path to the root of your drive and then ... walk down the path to your file, building the relative path as you go along. ... > Sorry for being so vague Rob ...
    (microsoft.public.dotnet.languages.csharp)
  • lock on filename for XmlDocument.Save?
    ... I'm calling XmlDocument.Save with a filename that often varies. ... sometimes absolute path and sometimes relative path. ... get a re-entrant save to the same filename, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need script to download file at known address
    ... Jeff Shannon wrote: ... > it accepts a pathname, rather than just a filename, so you can feed it ... > an absolute path like your example or a relative path ... ...
    (comp.lang.python)
  • Re: Find out really (!) the path / getParent() not working
    ... Now if you want to get absolute path from relative path you can do ... fileName = new File; ... String path = fileName.getParent; ...
    (comp.lang.java.programmer)
  • Re: How does Require deal with multiple files of the same name in
    ... Jos van de Ven wrote: ... If a relative path is specified, there is at least one file in the ... Perhaps the same behaviour should take place when an absolute path is specified. ... If the variable is set to re-load then the first matching file ...
    (comp.lang.forth)