IO Exception未得到处理

时间:2013-01-15 12:29:30

标签: c# asp.net

if(!File.Exists(_logFilePath))
            {
                File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?>\r\n <AppXmlLogWritter></AppXmlLogWritter>");
            }   

 using (FileStream fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, 
           FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.Load(_logFilePath);
        XmlElement newelement = xmlDoc.CreateElement("LogData");
        XmlElement xmlLogID = xmlDoc.CreateElement("LogID");
        XmlElement xmlLogDateTime = xmlDoc.CreateElement("LogDateTime");
        XmlElement xmlLogType = xmlDoc.CreateElement("LogType");
        XmlElement xmlLogFlag = xmlDoc.CreateElement("LogFlag");
        XmlElement xmlLogApplication = xmlDoc.CreateElement("LogApplication");
        XmlElement xmlLogModule = xmlDoc.CreateElement("LogModule");
        XmlElement xmlLogLocation = xmlDoc.CreateElement("LogLocation");
        XmlElement xmlLogText = xmlDoc.CreateElement("LogText");
        XmlElement xmlLogStackTrace = xmlDoc.CreateElement("LogStackTrace");

        xmlLogID.InnerText = _logIDPrefix + currentDateTime + randomNumber;
        xmlLogDateTime.InnerText = currentDateTime;
        xmlLogType.InnerText = ((LogTypes)Convert.ToInt32(logType)).ToString();
        xmlLogFlag.InnerText = logFlag;
        xmlLogApplication.InnerText = _logApplication;
        xmlLogModule.InnerText = logModule;
        xmlLogLocation.InnerText = logLocation;
        xmlLogText.InnerText = logText;
        xmlLogStackTrace.InnerText = logStackTrace;

        newelement.AppendChild(xmlLogID);
        newelement.AppendChild(xmlLogDateTime);
        newelement.AppendChild(xmlLogType);
        newelement.AppendChild(xmlLogFlag);
        newelement.AppendChild(xmlLogApplication);
        newelement.AppendChild(xmlLogModule);
        newelement.AppendChild(xmlLogLocation);
        newelement.AppendChild(xmlLogText);

        xmlDoc.DocumentElement.AppendChild(newelement);
        xmlDoc.Save(_logFilePath);
    }

如何在xmlDoc.Load(_logFilePath);

行上克服以下错误
  

该进程无法访问该文件,因为该文件正由另一个进程使用。

3 个答案:

答案 0 :(得分:1)

发生错误是因为您使用带有读写锁的FileStream打开文件,而在尝试使用xmlDoc.Load()方法读取文件后不久。由于文件被FileStream锁定,因此会导致异常。

您似乎无缘无故地打开FileStream,因为您没有使用它。使用FileStream简单地删除using语句。

删除它之后,剩下的是xmlDoc.Load()调用以及稍后的xmlDoc.Save()调用。由于您使用这些方法直接加载文件,并且您没有将文件锁定超过必要的时间。这应该有用。

答案 1 :(得分:0)

即使你说加载XML会给你带来这个错误,我也会看到你需要纠正的一个主要缺陷。在xmlDoc块之外声明using,并以同样的方式在其外部调用xmlDoc.Save

示例:

var xmlDoc = new XmlDocument();
using (var fileStream = new FileStream(_logFilePath, FileMode.Open,
FileAccess.Read, FileShare.None))
{
    xmlDoc.Load(fileStream);
    //Do the rest
}
xmlDoc.Save(_logFilePath);

编辑:我刚刚注意到您甚至没有使用您创建的FileStream。你可以删除它的声明。

var xmlDoc = new XmlDocument();
xmlDoc.Load(_logFilePath);
//Do the rest
xmlDoc.Save(_logFilePath);

答案 2 :(得分:0)

Ok和Eve和Maarten之前的2个答案相结合。如果您使用的是FileStream,请使用XmlDocument.Load方法而不是当前使用的_logFilePath变量。如果您使用FileStream,那么您将无法获得独占的读/写访问错误,但如果您只是使用文件位置,您将会。因此,正如Eve指出的那样,将代码更改为:

using (FileStream fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, 
       FileAccess.ReadWrite, FileShare.ReadWrite))
{
    string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load(fileStream); //instead of xmlDoc.Load(_logFilePath)

    //other stuff

}
相关问题