File being used by another process

  • Thread starter Thread starter mikes
  • Start date Start date
M

mikes

Hello,
I am building a c# web app that uses serialization to generate xml
files within a page load event. The page support both creation and
modification events. I do not have a problem with the creation
function. When I resubmit the page to mod the previously created xml
file I get "file xxxx is being used by another process." It appears
that all object have be correctly closed but the XmlTextWriter or the
FileStream objects have not terminated even after being closed.

Code is included below.

Do I need to use a mutex? Or should I set up a watcher to throw an
event on file state change.

Any help appreciated

if(Request.QueryString["funct"].ToString() == "add")
{
... process form vars.
.. intialize struct


XmlSerializer serializer = new
XmlSerializer(typeof(Common.Components.Utilities.MinistryContent));

FileStream fs = File.OpenWrite(strFileDirectory + strFileName);
XmlTextWriter writer = new XmlTextWriter(fs,
System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;

try
{
// perform the XML serialization
serializer.Serialize(writer, ministryContent);
}
finally
{
// close the writer, which closes the underlying FileStream
//writer.BaseStream.Close();
writer.Flush();
writer.Close();
fs.Close();
}

try
{
... do some db
}catch(System.exception ex)
{
.. exhandler
}

}
 
Fixed.

FileStream fsOpenWrite = new FileStream(m_strFileDirectory +
strFileName, FileMode.Open,FileAccess.Write,FileShare.Write);
 
Back
Top