Problem deleting temporary

I have an application that attempts to render MS Word documents (stored in a database BLOB) as a PDF to the client browser. This works well but I’m finding that it’s occasionally unable to delete the temporary files generated during the conversion process (this happens most often when processing very large documents) as though the conversion isn’t releasing the lock on the tempoary files after rendering. This results in some very large files being left in the TEMP folder which over time starts to consume a lot of disk space. My code looks like this:

DirectoryInfo tmpPath = new DirectoryInfo(Environment.GetEnvironmentVariable(“TEMP”));
string filename = DateTime.Now.Ticks.ToString();

FileInfo sourceFile = new FileInfo(Path.Combine(tmpPath.FullName, filename + “.doc”));
FileInfo xmlFile = new FileInfo(Path.Combine(tmpPath.FullName, filename + “.xml”));
FileInfo pdfFile = new FileInfo(Path.Combine(tmpPath.FullName, filename + “.pdf”));

FileStream theStream = File.Create(sourceFile.FullName.ToString());
BinaryWriter theWriter = new BinaryWriter(theStream);
BinaryReader theReader = new BinaryReader(theDoc);

theReader.BaseStream.Seek(0, SeekOrigin.Begin);

while (theReader.BaseStream.Position < theReader.BaseStream.Length)
{
theWriter.Write(theReader.ReadBytes(4096));
}

theReader.Close();
theWriter.Close();
theStream.Close();

Aspose.Word.License wlic = new Aspose.Word.License();
wlic.SetLicense(@Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Aspose.Total.lic”));

Aspose.Word.Document wDoc = new Aspose.Word.Document(sourceFile.FullName);

wDoc.Save(xmlFile.FullName, SaveFormat.FormatAsposePdf);

Aspose.Pdf.License plic = new Aspose.Pdf.License();
plic.SetLicense(@Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Aspose.Total.lic”));

Aspose.Pdf.Pdf pdfDoc = new Aspose.Pdf.Pdf();

pdfDoc.BindXML(xmlFile.FullName, null);
pdfDoc.Save(pdfFile.FullName);

// seems to fail here:
sourceFile.Delete();
xmlFile.Delete();

ProcessDocPackRequest(pdfFile.FullName, context);

Is there a way of ensuring that the temporary files will be deleted apart from implementing some asynchronous process monitoring solution?

Hi,

Thank you for considering Aspose.

Can you please tell me the err occurs on which type of file? Word, xml or image?

It seems to be the XML file that it falls over on. Like I said, it’s an intermittent problem which suggests that it’s environmentally-influenced although I’m not sure why that would be the case in a single-threaded process(?) A secondary but related issue is the cleaning up of any image files that are generated during the conversion process? How can I identify and delete these following generation of the PDF?

It is not easy for me to check this problem if I can’t reproduce the error. For the temp images, you can set IsImagesInXmlDeleteNeeded to true.