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

How can I overcome the below error, on line xmlDoc.Load(_logFilePath);

The process cannot access the file because it is being used by another process.

lax
  • 518
  • 2
  • 11
  • 26

3 Answers3

1

The errors occurs because you are opening a file using a FileStream with a read-write-lock, while shortly after you are trying to read the file using the xmlDoc.Load() method. Since the file is locked by the FileStream, this results in the exception.

You seem to open a FileStream for no reason, since you are not using it. Simple remove the using statement with the FileStream.

After you remove it, what is left is an xmlDoc.Load() call and later an xmlDoc.Save() call. Since you are using those methods to directly load the file, and you are not locking the file for any longer than necessary. That should work.

Maarten
  • 22,527
  • 3
  • 47
  • 68
  • Maarten i have to use locking because different applications use xml file.:( – lax Jan 15 '13 at 13:08
  • Then your best bet would be to use the Load/Save overloads which accept a stream, since you can open the FileStream in ReadWrite file-mode. See http://stackoverflow.com/questions/3817477/simultaneous-read-write-a-file-in-c-sharp. – Maarten Jan 15 '13 at 13:28
  • I used marten using(FileStream fileStream = new FileStream (_logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { – lax Jan 15 '13 at 13:34
  • Looks good, but are you also using the stream in the xmlDoc.Load en xmlDoc.Save methods? – Maarten Jan 15 '13 at 14:42
0

Even if you said that loading the XML gives you this error, I see one major flaw which you need to correct. Declare xmlDoc outside of the using block and, in the same way, call xmlDoc.Save outside of it.

Example:

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

EDIT: I've just noticed that you're not even using the FileStream you create. You can remove its declaration.

var xmlDoc = new XmlDocument();
xmlDoc.Load(_logFilePath);
//Do the rest
xmlDoc.Save(_logFilePath);
e_ne
  • 8,340
  • 32
  • 43
  • @lax I've edited my answer. Please see if it can help you now. – e_ne Jan 15 '13 at 12:39
  • I have to use filestream for locking purpose because xml log writes by several different application.............. – lax Jan 15 '13 at 12:40
  • @lax Then perhaps in the first case I brought your file is already open somewhere else. I've changed its flags passed to the constructor, please see if it helps. Otherwise, try to verify if your file is already open somewhere else. Furthermore, surround the whole block in a `try/catch`. – e_ne Jan 15 '13 at 12:43
  • @lax See [this question](http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) for instructions on how to do that. – e_ne Jan 15 '13 at 12:44
  • you weren't getting the same error lax, I'm sure of that, you were using the file path variable when you should have been using the `fileStream` variable. – anothershrubery Jan 15 '13 at 12:51
  • But file is not correctly write heade is again write with Root elemnt what should i do.. – lax Jan 15 '13 at 12:56
0

Ok a combination of the 2 previous answers from Eve and Maarten. If you are using the FileStream then use it in the XmlDocument.Load method rather than the _logFilePath variable you are currently using. If you use the FileStream then you will not get the exclusive read/write access error, but if you just use teh file location, you will. So, as Eve indicated, change your code to:

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

}
anothershrubery
  • 20,461
  • 14
  • 53
  • 98
  • Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 4, position 22. – lax Jan 15 '13 at 12:53
  • file is write like this 5678201301151823341051 20130115182334 Warning BaburaoLogFlag BaburaoLogApplication BaburaoLogModule BaburaoLogLocation BaburaoLogText – lax Jan 15 '13 at 12:54
  • file is not correctly write header is again write with Root element what should i do.. – lax Jan 15 '13 at 12:56
  • @lax You've downvoted my answer (without actually trying the approach, since it's the same as here) and now it turns out that you are not even using correctly formatted XML. I think you should ask a question after you know what you need and what you are doing. – e_ne Jan 15 '13 at 12:57
  • @lax The approach I've offered below works correctly for me and I get no duplicated roots. – e_ne Jan 15 '13 at 13:03
  • @Eve if file is not exist then check my question i have updated – lax Jan 15 '13 at 13:05
  • when xmlDoc.Load(fileStream); it gets whole xml file with header and root... thats why getting error see i have paste my xml file below of anothershrubery answer – lax Jan 15 '13 at 13:06
  • @lax I've seen what you do when the file doesn't exist, but if I do that, the error doesn't happen for me. Try using this code: http://eve.cx/paste/Q7dr9eEn.txt – e_ne Jan 15 '13 at 13:09
  • @Eve when i consume xmlLogwriter class in other application then again throw me same exception The process cannot access the file because it is being used by another process. Locking is not work – lax Jan 15 '13 at 13:17
  • @lax That is, indeed, the expected behavior when operating on a locked file. As I said, surround the whole thing in a `try/catch` block. I'm sorry but I'm afraid I can't help you further. – e_ne Jan 15 '13 at 13:23
  • @Eve i cant lock processes using filestream – lax Jan 15 '13 at 13:25
  • With regards to your XML problem, it is a separate issue, but if you actually look at the XML you have, as the error described, declared the xml twice. Removing the first of these declarations, ` `, provides you with the correct result. – anothershrubery Jan 15 '13 at 13:41