create and write to file

M

Mike P

I am trying to create a new file and write to it, but I keep getting the
error 'cannot access the file because it is being used by another
process'. Can anybody help me out?

long lngNextNumber = Find_Next_File_Number();

string strNewFile =
"C:\\inetpub\\wwwroot\\webapplication90\\xml_files\\test" +
lngNextNumber + ".xml";
File.Create(strNewFile);


//create the XMLTextWriter
XmlTextWriter tw = new XmlTextWriter(strNewFile, null);

//set the formatting to indented
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();

//start creating elements and attributes
tw.WriteStartElement("book");
tw.WriteElementString("title", "Case of the missing cookie");
tw.WriteEndElement();
tw.WriteEndDocument();

//clean up
tw.Flush();
tw.Close();


Cheers,

Mike
 
R

Rami Saad

Hi,
simply remove this line: File.Create(strNewFile);
If you create a File object, then it's being used, and u can't create an
XMLTextWriter with the same path and filename
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top