XmlDoc 有时会抛出进程无法访问文件?

XmlDoc throws the process cannot access the file from tome to time?

我有以下代码用于根据数据合同编写XML文件

public static void LogDataContractToFile(string XMLStringToLog, string filePathAndName)
        {
            //String documentPath = string.Empty;
            String xmlObject = string.Empty;

            FileInfo fileinfo;

            XmlDocumentFragment xmlDocumentFragment;
            XmlTextWriter xmlWriter;
            XmlDocument xmlDocument = null;

            lock (LogDataContractToFileLock)
            {

                filePathAndName = filePathAndName.ToLower();

                while (_workingWithFile.Contains(filePathAndName))
                    Thread.Sleep(1000);

                _workingWithFile.Add(filePathAndName.ToLower());

                try
                {

                    #region Create XMLFile
                    fileinfo = new FileInfo(filePathAndName);

                    if (!fileinfo.Exists)
                    {
                        DirectoryInfo info = new DirectoryInfo(fileinfo.DirectoryName);
                        if (info.Exists == false)
                            info.Create();

                        using (xmlWriter = new XmlTextWriter(filePathAndName, System.Text.Encoding.UTF8))
                        {
                            xmlWriter.Formatting = Formatting.Indented;
                            xmlWriter.WriteStartDocument();
                            xmlWriter.WriteStartElement("root");
                            xmlWriter.WriteStartElement("objects");
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteEndDocument();
                            xmlWriter.Close();
                        }
                    }
                    else
                    {
                        //Se så att filen är 50 MB eller mindre
                        while (fileinfo.Length > 52428800)
                        {
                            xmlDocument = new XmlDocument();
                            xmlDocument.Load(filePathAndName);

                            xmlDocument.RemoveChild(xmlDocument.LastChild);
                            xmlDocument.Save(filePathAndName);
                            xmlDocument = null;
                        }
                    }
                    #endregion

                    xmlObject = XMLStringToLog;

                    //Open document
                    xmlDocument = new XmlDocument();
                    xmlDocument.Load(filePathAndName);

                    //Create a new fragment in current document
                    xmlDocumentFragment = xmlDocument.CreateDocumentFragment();
                    xmlDocumentFragment.InnerXml = xmlObject;

                    //Add new fragment after the first child
                    xmlDocument.DocumentElement.InsertBefore(xmlDocumentFragment, xmlDocument.DocumentElement.FirstChild);
                    xmlDocument.Save(filePathAndName);
                    xmlDocument = null;
                }
                finally
                {
                    _workingWithFile.Remove(filePathAndName.ToLower());
                }
            }
        }

问题是我时常遇到 The process cannot access the file 异常? XmlDocument 没有任何处置,所以我不能使用。这应该如何正确处理?

请注意,我卡在 .NET 4.0 上。

要安全地检查一个元素并在它不存在时添加它,您应该使用 ConcurrentDictionary:

private readonly ConcurrentDictionary<string,bool> _workingWithFile = new ConcurrentDictionary<string,bool>();
public static void LogDataContractToFile(string XMLStringToLog, string filePathAndName)
{
    ...
    lock (LogDataContractToFileLock)
    {
        ...

        while(!_workingWithFile.TryAdd(filePathAndName, true))
        {
            Thread.Sleep(1000);
        }
        ...
        try
        {
            ...
        }
        finally
        {
            //Perhaps check the result here.
            bool result;
            _workingWithFile.TryRemove(filePathAndName, out result);
        }
    }
}