-3
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);

ファイルが別のプロセスによって使用されているため、プロセスはファイルにアクセスできません。

4

3 に答える 3

1

エラーは、xmlDoc.Load() メソッドを使用してファイルを読み取ろうとした直後に、読み取り/書き込みロックで FileStream を使用してファイルを開こうとしているために発生します。ファイルは FileStream によってロックされているため、例外が発生します。

FileStream を使用していないため、理由もなく開いているようです。FileStream を使用して using ステートメントを単純に削除します。

削除すると、xmlDoc.Load() 呼び出しと、後で xmlDoc.Save() 呼び出しが残ります。これらの方法を使用してファイルを直接ロードしているため、必要以上にファイルをロックしていません。それはうまくいくはずです。

于 2013-01-15T12:40:14.523 に答える
0

XML をロードするとこのエラーが発生するとおっしゃっていたとしても、修正が必要な大きな欠陥が 1 つあります。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);
于 2013-01-15T12:33:50.883 に答える
0

Eve と Maarten からの 2 つの以前の回答の組み合わせでOKです。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

}
于 2013-01-15T12:49:23.327 に答える