-1

I have a C# console application which creates, parses and deletes multiple xml files at runtime. The application used to run fine in Windows 2003 server with .Net 2.0. Recently, the Application framework was upgraded to >net 4.0 and the Windows Server OS to Windows 2008 64-bit.

Since then, the application encounters the following exception at random:

Access to the path 'D:\Content\iSDC\GDCOasis\GATE_DATA\LOG\635125008068192773\635125008074911566\SOD\AllRespId.xml' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at ProcessGateFile.SOD.saveFile(String psFile, String psXMLString, Boolean isNonAscii)

The code for the creation, parsing and deletion is as follows:

saveFile(tmpPath + "\\SOD\\AllRespId.xml", "<?xml version= \"1.0\" ?><XML>" + sbldDistinctResp.ToString() + "</XML>", isChinese);

//Save list of Distinct responsibilities for User
sbldDistinctResp.Remove(0, sbldDistinctResp.Length);
xmlCase.Load(tmpPath + "\\SOD\\AllRespId.xml");
arrResps.Clear();

//Start preparing Responsibility selection criteria
RespNodes = xmlCase.SelectNodes("//row");
sRespCriteria = "";

if (RespNodes.Count > 0)
{
    foreach (XmlNode RespNode in RespNodes)
    {
        string RespName = RespNode.Attributes.GetNamedItem("RespId").Value.ToString();
        if (!arrResps.Contains(RespName))
        {
             arrResps.Add(RespName);
        }
    }
    for (int i = 0; i < arrResps.Count; i++)
    {
         sbldDistinctResp.Append("(@RespId = '" + arrResps[i].ToString() + "') or ");
    }
    sbldDistinctResp.Remove(sbldDistinctResp.Length - 4, 4);
    sRespCriteria = sbldDistinctResp.ToString();

    if (!sRespCriteria.Equals(""))
    {
         sRespCriteria = "(" + sRespCriteria + ")";
    }
 }
 File.Delete(tmpPath + "\\SOD\\AllRespId.xml");

I repeat, the error is happening at random, i.e. it works at times and does not at other times during the same process.

Any idea what might be causing this and how to resolve?

Tobias
  • 232
  • 1
  • 16
Bedabrata
  • 15
  • 1

1 Answers1

0

Just a couple of observations:

  1. Why are you saving and then immediately loading the file again? In fact, why do you even need to save this file - you already have all the information you need in the sbldDistinctResp variable to generate the XML you need to work with (as evidenced by the saveFile call at the start of the code) - couldn't you just make a copy of it, surround it with the same XML as you did during saveFile, and work with that?

  2. "It happens randomly" is a very subjective observation :). You should profile this (run it 10,000 times in a loop for example) and record the pattern of errors. You may well be surprised that what seems random at first actually shows a clear pattern over a large number of runs. This may help you to make a connection between the problem and some other apparently unrelated event on the server; or it may confirm that it truly is random and therefore outside of your control.

  3. If you really can't find the problem and you go with the idea of anti-virus, etc, then you could wrap the loading code in a try/catch and re-try a couple of times if you get the error. It's hacky but it would work, assuming you have accepted that the initial error is beyond your control.

Stephen Byrne
  • 7,400
  • 1
  • 31
  • 51