错误无法访问关闭文件

时间:2013-01-16 13:46:20

标签: c# xml

尝试使用FileStream.BeginWrite()方法编写xml文件,但它为我提供了无法访问关闭文件错误

我的代码是

public void WriteXmlLog(string logType, string logFlag, string logModule, string logLocation, string logText, string logStackTrace)
{
    if (!File.Exists(_logFilePath))
    {
        File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?>\r\n<AppXmlLogWritter></AppXmlLogWritter>");
    }

    XmlDocument xmlDoc = new XmlDocument();

    using (fileStream = new FileStream(_logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        xmlDoc.Load(fileStream);
        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);
        Byte[] myByteArray = Encoding.UTF8.GetBytes(newelement.ToString());
        fileStream.BeginWrite(myByteArray, 0, myByteArray.Length, WriteAsyncCallback, new LogWritter(myByteArray, fileStream));
    }

    xmlDoc.Save(_logFilePath);
}

public Stream MyStream { get; set; }

private void WriteAsyncCallback(IAsyncResult ar)
{
    LogWritter info = ar.AsyncState as LogWritter;
    info.MyStream.EndWrite(ar);
}

WriteAsyncCallback(IAsyncResult ar)函数给出了上述错误。

2 个答案:

答案 0 :(得分:2)

您已在using()语句中打开了流,但您在该语句中执行的最后一项操作是发出异步写入。好吧,只要您发出异步写入,using语句就会终止,您的文件将在异步写入发生之前关闭。不要在这里做异步写。除非你保持文件打开并且在异步写入完成后有一个处理程序来关闭它,否则它不会给你带来任何好处。

答案 1 :(得分:1)

你通过在这一行开始一个后台线程来让你的using语句超出范围:

fileStream.BeginWrite(myByteArray, 0, myByteArray.Length, WriteAsyncCallback, new LogWritter(myByteArray, fileStream));

如果您希望此函数是线程安全的,您可能需要考虑线程锁定WriteXmlLog方法。